Inheritance and PolymorphismReview: ClassesExample: Shared FunctionalitySlide 4InheritanceInheritance (continued)Inheritance: class Circle Derived from class ShapeSlide 8Code ReuseInheritance: Adding FunctionalitySlide 11BrainstormingUML Diagram: RectangleObjects myRectangle and myBoxUML Class Diagram: class BoxOverriding Methodsclass Rectanglefinal MethodsCalling methods of the superclassclass BoxDefining Constructors of the SubclassAccess ControlPolymorphismSlide 24Polymorphism (continued)Polymorphism and ReferencesSlide 27CastingSlide 29Abstract MethodsAbstract ClassesAbstract Classes (continued)Abstract Class ExampleInterfacesCompositionComposition ExampleComposition Example (continued)Chris KiekintveldCS 2401 (Fall 2010) Elementary Data Structures and AlgorithmsInheritance and PolymorphismReview: ClassesUser-defined data typesDefined using the “class” keywordEach class has associatedData members (any object type)Methods that operate on the dataNew instances of the class are declared using the “new” keyword“Static” members/methods have only one copy, regardless of how many instances are createdJava Programming: Program Design Including Data Structures 2Example: Shared FunctionalityJava Programming: Program Design Including Data Structures 3public class Student { String name; char gender; Date birthday; Vector<Grade> grades; double getGPA() { … } int getAge(Date today) { … } }public class Professor { String name; char gender; Date birthday; Vector<Paper> papers; int getCiteCount() { … } int getAge(Date today) { … } }Java Programming: Program Design Including Data Structures 4public class Person { String name; char gender; Date birthday; int getAge(Date today) { … } }public class Student extends Person { Vector<Grade> grades; double getGPA() { … }}public class Professor extends Person { Vector<Paper> papers; int getCiteCount() { … } }Java Programming: Program Design Including Data Structures 5Inheritance“is-a” relationshipSingle inheritance: Subclass is derived from one existing class (superclass)Multiple inheritance:Subclass is derived from more than one superclassNot supported by JavaA class can only extend the definition of one classJava Programming: Program Design Including Data Structures 6Inheritance (continued)modifier(s) class ClassName extends ExistingClassName modifier(s){ memberList}Java Programming: Program Design Including Data Structures 7Inheritance: class Circle Derived from class Shapepublic class Circle extends Shape{ . . .}InheritanceAllow us to specify relationships between typesAbstraction, generalization, specificationThe “is-a” relationshipExamples?Why is this useful in programming?Allows for code reuseMore intuitive/expressive codeCode ReuseGeneral functionality can be written once and applied to *any* subclassSubclasses can specialize by adding members and methods, or overriding functionsInheritance: Adding FunctionalitySubclasses have al l of the data members and methods of the superclassSubclasses can add to the superclassAdditional data membersAdditional methodsSubclasses are more specific and have more functionalitySuperclasses capture generic functionality common across many types of objectsJava Programming: Program Design Including Data Structures 10Java Programming: Program Design Including Data Structures 11public class Person { String name; char gender; Date birthday; int getAge(Date today) { … } }public class Student extends Person { Vector<Grade> grades; double getGPA() { … }}public class Professor extends Person { Vector<Paper> papers; int getCiteCount() { … } }BrainstormingWhat are some other examples of possible inheritance hierarchies?Person -> student, faculty…Shape -> circle, triangle, rectangle…Other examples???Java Programming: Program Design Including Data Structures 12Java Programming: Program Design Including Data Structures 13UML Diagram: RectangleWhat if we want to implement a 3d box object?Java Programming: Program Design Including Data Structures 14Objects myRectangle and myBoxRectangle myRectangle = new Rectangle(5, 3);Box myBox = new Box(6, 5, 4);Java Programming: Program Design Including Data Structures 15UML Class Diagram: class BoxBoth a Rectangle and a Box have a surface area,but they are computed differentlyOverriding MethodsA subclass can override (redefine) the methods of the superclassObjects of the subclass type will use the new methodObjects of the superclass type will use the originalJava Programming: Program Design Including Data Structures 16Java Programming: Program Design Including Data Structures 17class Rectanglepublic double area(){ return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height);}class Boxpublic double area(){ return getLength() * getWidth();}Java Programming: Program Design Including Data Structures 18final MethodsCan declare a method of a class final using the keyword finalpublic final void doSomeThing(){ //...}If a method of a class is declared final, it cannot be overridden with a new definition in a derived classJava Programming: Program Design Including Data Structures 19Calling methods of the superclassTo write a method’s definition of a subclass, specify a call to the public method of the superclassIf subclass overrides public method of superclass, specify call to public method of superclass: super.MethodName(parameter list)If subclass does not override public method of superclass, specify call to public method of superclass: MethodName(parameter list)Java Programming: Program Design Including Data Structures 20class Boxpublic void setDimension(double l, double w, double h){ super.setDimension(l, w); if (h >= 0) height = h; else height = 0;}}Box overloads the method setDimension(Different parameters)Java Programming: Program Design Including Data Structures 21Defining Constructors of the SubclassCall to constructor of superclass: Must be first statementSpecified by super parameter listpublic Box(){ super(); height = 0;}public Box(double l, double w, double h){ super(l, w); height = h;}Access ControlAccess control keywords define which classes can access classes, methods, and membersJava Programming: Program Design Including
View Full Document