Topic 6 Topic 6 Inheritance and Inheritance and Polymorphism "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.” CS 307 Fundamentals of 1 Computer Science Inheritance and Polymorphism
Outline 8 Explanation of inheritance. 8 Using inheritance to create a SortedIntList. 8 Explanation of polymorphism. 8 Using polymorphism to make a more generic Using polymorphism to make a more generic List class. CS 307 Fundamentals of 2 Computer Science Inheritance and Polymorphism
Explanation of Inheritance Explanation of Inheritance CS 307 Fundamentals of 3 Computer Science Inheritance and Polymorphism
Main Tenets of OO Programming 8 Encapsulation – abstraction, information hiding abs ac o , o a o d g 8 Inheritance – code reuse, specialization New code using old code reuse specialization "New code using old code." 8 Polymorphism 8 Polymorphism – do X for a collection of various types of objects, where X is different depending on the type of where X is different depending on the type of object – "Old code using new code " Old code using new code. CS 307 Fundamentals of 4 Computer Science Inheritance and Polymorphism
Things and Relationships 8 Obj 8 Object oriented programming leads to t i t d i l d t programs that are models – sometimes models of things in the real world ti d l f thi i th l ld – sometimes models of contrived or imaginary things 8 There are many types of relationships between 8 There are many types of relationships between the things in the models – chess piece has a position chess piece has a position – chess piece has a color – chess piece moves (changes position) chess piece moves (changes position) – chess piece is taken – a rook is a type of chess piece a rook is a type of chess piece CS 307 Fundamentals of 5 Computer Science Inheritance and Polymorphism
The “has-A” Relationship 8 Objects are often made up of many parts or have sub data. – chess piece: position, color – die: result, number of sides 8 This “has-a” relationship is modeled by composition p – the instance variables or fields internal to objects 8 Encapsulation captures this concept Encapsulation captures this concept CS 307 Fundamentals of 6 Computer Science Inheritance and Polymorphism
The “is-a” relationship 8 Another type of relationship found in the real world – a rook is a chess piece – a queen is a chess piece – a student is a person – a faculty member is a person – an undergraduate student is a student 8 “is-a” usually denotes some form of is a usually denotes some form of specialization 8 it is not the same as “has-a” it is not the same as has-a CS 307 Fundamentals of 7 Computer Science Inheritance and Polymorphism
Inheritance 8 The “is-a” relationship, and the specialization that accompanies it, is modeled in object oriented languages via inheritance 8 Classes can inherit from other classes – base inheritance in a program on the real world things being modeled – does “an A is a B” make sense? Is it logical? CS 307 Fundamentals of 8 Computer Science Inheritance and Polymorphism
Nomenclature of Inheritance 8 In Java the extends keyword is used in the 8 I J th k d i d i th t d class header to specify which preexisting class a new class is inheriting from a e c ass s e g o public class Student extends Person 8 Person is said to be – the parent class of Student h l f S d – the super class of Student – the base class of Student – an ancestor of Student 8 Student is said to be – a child class of Person – a sub class of Person – a derived class of Person – a descendant of Person CS 307 Fundamentals of 9 Computer Science Inheritance and Polymorphism
Results of Inheritance public class A public class B extends A 8 the sub class inherits (gains) all instance variables and instance methods of the super a ab es a d sta ce et ods o t e supe class, automatically 8 additional methods can be added to class B additional methods can be added to class B (specialization) 8 the sub class can replace (redefine 8 the sub class can replace (redefine, override) methods from the super class CS 307 Fundamentals of 10 Computer Science Inheritance and Polymorphism
Attendance Question 1 What is the primary reason for using inheritance when programming? A. To make a program more complicated B. To duplicate code between classes C To reuse pre-existing code C. To reuse pre-existing code D. To hide implementation details of a class E. To ensure pre conditions of methods are met. CS 307 Fundamentals of 11 Computer Science Inheritance and Polymorphism
Inheritance in Java 8 Java is a pure object oriented language Java is a pure object oriented language 8 all code is part of some class 8 all classes except one must inherit from 8 all classes, except one, must inherit from exactly one other class 8 The Object class is the cosmic super class The Object class is the cosmic super class – The Object class does not inherit from any other class – The Object class has several important methods: toString , equals , hashCode , clone , getClass 8 implications: – all classes are descendants of Object all classes are descendants of Object – all classes and thus all objects have a toString , equals , hashCode , clone , and getClass method toString , equals , hashCode , clone normally overridden • CS 307 Fundamentals of 12 Computer Science Inheritance and Polymorphism
Inheritance in Java 8 If 8 If a class header does not include the l h d d t i l d th extends clause the class extends the Object class by default t class by default Obj public class Die – Object is an ancestor to all classes i ll l – it is the only class that does not extend some other class th l 8 A class extends exactly one other class – extending two or more classes is multiple inheritance. Java does not support this directly, rather it uses Interfaces . th it I t f CS 307 Fundamentals of 13 Computer Science Inheritance and Polymorphism
Overriding methods 8 any method that is not final may be overridden by a descendant class y 8 same signature as method in ancestor 8 may not reduce visibility 8 may not reduce visibility 8 may use the original method if simply want to add more behavior to existing dd b h i t i ti CS 307 Fundamentals of 14 Computer Science Inheritance and Polymorphism
Attendance Question 2 What is output when the main method is run? public class Foo{ public static void main(String[] args){ Foo f1 = new Foo(); System.out.println( f1.toString() ); } } A. 0 B null B. null C. Unknown until code is actually run. D. No output due to a syntax error. E. No output due to a runtime error. p CS 307 Fundamentals of 15 Computer Science Inheritance and Polymorphism
Shape Classes 8 D 8 Declare a class called ClosedShape l l ll d – assume all shapes have x and y coordinates – override Object 's version of toString 8 Possible sub classes of ClosedShape – Rectangle – Circle – Ellipse – Square Square 8 Possible hierarchy ClosedShape < Rectangle < Square ClosedShape <- Rectangle <- Square CS 307 Fundamentals of 16 Computer Science Inheritance and Polymorphism
A ClosedShape class public class ClosedShape p p { private double myX; private double myY; public ClosedShape() public ClosedShape() { this(0,0); } public ClosedShape (double x, double y) { { myX = x; myX x; myY = y; } public String toString() bli i i () { return "x: " + getX() + " y: " + getY(); } public double getX(){ return myX; } p g y public double getY(){ return myY; } } // Other methods not shown CS 307 Fundamentals of 17 Computer Science Inheritance and Polymorphism
Constructors 8 Constructors handle initialization of objects 8 Constructors handle initialization of objects 8 When creating an object with one or more ancestors (every type except Object) a chain of constructor calls takes place yp p j ) p 8 The reserved word super may be used in a constructor to call a one of the parent's constructors – must be first line of constructor t b fi t li f t t 8 if no parent constructor is explicitly called the default, 0 parameter constructor of the parent is called p p – if no default constructor exists a syntax error results 8 If a parent constructor is called another constructor in the same class ma no be called same class may no be called – no super();this(); allowed. One or the other, not both – good place for an initialization method CS 307 Fundamentals of 18 Computer Science Inheritance and Polymorphism
A Rectangle Constructor public class Rectangle extends ClosedShape { private double myWidth; private double myHeight; public Rectangle( double x, double y, double width, double height ) g { super(x,y); // calls the 2 double constructor in // ClosedShape p myWidth = width; myHeight = height; } // other methods not shown } CS 307 Fundamentals of 19 Computer Science Inheritance and Polymorphism
More recommend