Unformatted text preview:

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: ClassesUser-defined data typesDefined using the “class” keywordEach class has associatedData members (any object type)Methods that operate on the dataNew 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” relationshipSingle inheritance: Subclass is derived from one existing class (superclass)Multiple inheritance:Subclass is derived from more than one superclassNot supported by JavaA 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{ . . .}InheritanceAllow us to specify relationships between typesAbstraction, generalization, specificationThe “is-a” relationshipExamples?Why is this useful in programming?Allows for code reuseMore intuitive/expressive codeCode ReuseGeneral functionality can be written once and applied to *any* subclassSubclasses can specialize by adding members and methods, or overriding functionsInheritance: Adding FunctionalitySubclasses have al l of the data members and methods of the superclassSubclasses can add to the superclassAdditional data membersAdditional methodsSubclasses are more specific and have more functionalitySuperclasses 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() { … } }BrainstormingWhat 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 MethodsA subclass can override (redefine) the methods of the superclassObjects of the subclass type will use the new methodObjects 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 MethodsCan 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 superclassTo write a method’s definition of a subclass, specify a call to the public method of the superclassIf 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 SubclassCall to constructor of superclass: Must be first statementSpecified by super parameter listpublic Box(){ super(); height = 0;}public Box(double l, double w, double h){ super(l, w); height = h;}Access ControlAccess control keywords define which classes can access classes, methods, and membersJava Programming: Program Design Including


View Full Document

UTEP CS 2401 - Inheritance Polymorphism

Documents in this Course
Load more
Download Inheritance Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?