CompSci 125 Lecture 20 Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams
Announcements
Introduction to Inheritance
Inheritance: Motivation Software is expensive Wish to define a new class by identifying only how it differs from an existing class Don t want to rewrite the entire existing class
Inheritance extends! Parent class Child inherits from parent Extend functionality Modify functionality Parent Class Child Class
Alternative Terminology Parent class Super class Base class Ancestor class Child class Subclass Derived class Heir class
Inheritance Example public class MyPanel extends JPanel {!!!//Child MyPanel inherits from JPanel parent!! }!
What does Child Inherit? Member Methods Member Variables Member Nested Classes (Not yet discussed in CS125) Not constructors
Thus If child does nothing but inherit, it s a replica of parent sans its constructors (more about this later) Perfectly legal: JComponent p = new JPanel();! Because JPanel inherits from JComponent Because JPanel is a JComponent JPanel implements JComponent s methods JPanel implements JComponent s variables
Child May Differ from Parent Methods Child may define methods not available in the parent Child may override methods defined by the parent Variables Child may define new variables not available in the parent Child may shadow parent s variables (unsafe practice) Constructors Child does not inherit parent s constructors Child may define its own constructors
Chained Inheritance Grandchildren inherit from child Grandchildren also inherit through child from parent Container JComponent JPanel
What About private Members? Child inherits parent s private variables and methods Child has no access to them (cannot reference them) Private members are present in the child but invisible
Parent Knows Nothing About Child Inheritance is a one-way street Parent has no knowledge or access to child s variables nor methods If class MyPanel extends JPanel, then JPanel knows nothing about the variables and methods in MyPanel
Instantiating Child does not Instantiate the Parent Class If your class MyPanel extends JPanel And if you construct ( new-up ) an instance of a MyPanel You built only a MyPanel; you did not also build a JPanel But a MyPanel implements JPanel s methods/variables
Child May add Functionality not Available in the Parent Class public class MyPanel extends JPanel {!!!private ArrayList<Point> mypoints;!!!public void drawmypoints() {!!!.!!!.!!!.!!} //drawpoints!! } //MyPanel!
Child May Override Functionality Defined by its Parent Class public class MyPanel extends JPanel {!!!public void paintcomponent(graphics g) {!!!.!!!.!!!.!!} //paintcomponent!! } //MyPanel!
Bad Practice: Child may even Override Variables public class MyPoint extends Point {!!!public double x, y;!.!!.!!.!! } //MyPoint!!//Shadow variables!
super! Refers to functionality defined by the parent class super.doit();!//invokes parent s doit() method! super();!!//invokes parent s constructor!
Example: Invoking a method defined in a parent class public class MyPanel extends JPanel {!!!public void paintcomponent(graphics g) {!!!super.paintcomponent(g);!!!.!!!.!//draw additional artwork here!!!.!!} //paintcomponent!! } //MyPanel!
Inheritance and Constructors Class members include instance variables and methods Members are inherited and can be overridden Constructors are not members Constructors are not inherited and thus cannot be overridden BUT a child s constructor can invoke a constructor defined in the parent class: super.parentclassname()!
Implicit invocation of super() If your constructor doesn t explicitly invoke a parent s constructor Then Java implicitly inserts: super();! Attempting to ensure the object instance variables defined in the parent class are initialized
Implicit super constructor ParentName() is undefined for default constructor. Must define an explicit constructor super() does not exist The child class must explicitly invoke one of the parent s constructors with parameters Or define a constructor without parameters in parent
Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor super() does not exist Child must explicitly invoke another constructor
UML: Class Diagrams
UML: Class Diagram Overview ClassName Attributes Operations Properties Attributes Associations Operations Methods Constructors
UML has been Rightfully Criticized for its Complexity Toad -x: int! -y: int! -health: int! +count: byte! +move(int,int)! +injure()! +heal()! +gethealth(): int! public class Toad {! //Attributes!! }!! private int x,y;! private int health;! public static byte count=0;! //Operations! public void move(int x, int y) { }! public void injure() { }! public void heal() { }! public static int gethealth() { }!
UML: Class Diagram Dependency Relationship Class1 Properties Operations Class2 Properties Operations Directed, dashed line indicates Class1 depends upon Class2 Transient relationship (as opposed to an association) Class1 invokes methods in Class2 thru a local variable Class1 invokes methods in Class2 thru a parameter
UML: Class Diagram Association Relationship Class1 Properties Operations Class2 Properties Operations Solid, directed line Fixed state relationship (as opposed to a dependency) Class2 appears as the type of an instance variable in Class1 Designer graphically emphasizes the importance of a property
Association or Attribute More or less the same thing! Both usually implemented in Java as instance variables Graphical association emphasizes important, controversial relationships, especially between new/modified classes List attributes suitable for built-in Java, non-controversial, and legacy types that won t change in the design process
UML: Class Diagram Inheritance Relationship AKA Generalization Class2 inherits from Class1 Class1 Properties Operations Class2 Properties Operations
UML Advice UML is important Fowler, M. UML Distilled. Addison Wesley. 2004. Be familiar with these: Class Diagrams Sequence Diagrams Use for design discussions on a whiteboard Capture with camera phone Keep it simple! Because you can doesn t mean you should include it Also: Formal design documentation http://www.shilman.net/cal//research/whysketch.html
Software Design: A Starter Definition What classes are required? How do those classes interact? Use class diagram to explore which classes instantiate another Use class diagram to explore which classes inherit from another When do their objects interact? Use sequence diagram to explore when objects are instantiated Use sequence diagram to explore when objects invoke methods