Outline Topic 6 Topic 6 � Explanation of inheritance. � Using inheritance to create a SortedIntList. Inheritance and Inheritance and � Explanation of polymorphism. Polymorphism � Using polymorphism to make a more generic Using polymorphism to make a more generic "Question: What is the object oriented way of List class. 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 CS 307 Fundamentals of 1 2 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism Main Tenets of OO Programming � Encapsulation – abstraction, information hiding abs ac o , o a o d g � Inheritance Explanation of Inheritance Explanation of Inheritance – code reuse, specialization New code using old code reuse specialization "New code using old code." � Polymorphism � 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 3 CS 307 Fundamentals of 4 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism
Things and Relationships The “has-A” Relationship � Object oriented programming leads to � Obj t i t d i l d t � Objects are often made up of many parts or programs that are models have sub data. – sometimes models of things in the real world ti d l f thi i th l ld – chess piece: position, color – sometimes models of contrived or imaginary things – die: result, number of sides � There are many types of relationships between � There are many types of relationships between � This “has-a” relationship is modeled by the things in the models composition p – chess piece has a position chess piece has a position – the instance variables or fields internal to objects – chess piece has a color � Encapsulation captures this concept Encapsulation captures this concept – 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 CS 307 Fundamentals of 5 6 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism The “is-a” relationship Inheritance � Another type of relationship found in the real � The “is-a” relationship, and the specialization world that accompanies it, is modeled in object oriented languages via inheritance – a rook is a chess piece � Classes can inherit from other classes – a queen is a chess piece – a student is a person – base inheritance in a program on the real world things being modeled – a faculty member is a person – does “an A is a B” make sense? Is it logical? – an undergraduate student is a student � “is-a” usually denotes some form of is a usually denotes some form of specialization � it is not the same as “has-a” it is not the same as has-a CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism
Nomenclature of Inheritance Results of Inheritance � In Java the extends keyword is used in the � I J th k d i d i th t d public class A class header to specify which preexisting class public class B extends A a new class is inheriting from a e c ass s e g o � the sub class inherits (gains) all instance public class Student extends Person � Person is said to be variables and instance methods of the super a ab es a d sta ce et ods o t e supe – the parent class of Student h l f S d class, automatically – the super class of Student – the base class of Student � additional methods can be added to class B additional methods can be added to class B – an ancestor of Student (specialization) � Student is said to be – a child class of Person � the sub class can replace (redefine, � the sub class can replace (redefine – a sub class of Person override) methods from the super class – a derived class of Person – a descendant of Person CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism Inheritance in Java Attendance Question 1 � Java is a pure object oriented language Java is a pure object oriented language What is the primary reason for using � all code is part of some class inheritance when programming? � all classes, except one, must inherit from � all classes except one must inherit from exactly one other class A. To make a program more complicated � The Object class is the cosmic super class The Object class is the cosmic super class B. To duplicate code between classes – The Object class does not inherit from any other class – The Object class has several important methods: C. To reuse pre-existing code C To reuse pre-existing code toString , equals , hashCode , clone , getClass D. To hide implementation details of a class � implications: – all classes are descendants of Object all classes are descendants of Object E. To ensure pre conditions of methods are met. – 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 11 CS 307 Fundamentals of 12 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism
Overriding methods Inheritance in Java � If a class header does not include the � If l h d d t i l d th � any method that is not final may be extends clause the class extends the overridden by a descendant class y Object class by default t class by default Obj � same signature as method in ancestor public class Die � may not reduce visibility � may not reduce visibility – Object is an ancestor to all classes i ll l � may use the original method if simply want to – it is the only class that does not extend some add more behavior to existing dd b h i t i ti other class th l � 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 CS 307 Fundamentals of 13 14 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism Attendance Question 2 Shape Classes � Declare a class called ClosedShape � D l l ll d What is output when the main method is run? public class Foo{ – assume all shapes have x and y coordinates public static void main(String[] args){ Foo f1 = new Foo(); – override Object 's version of toString System.out.println( f1.toString() ); } � Possible sub classes of ClosedShape } – Rectangle A. 0 – Circle B. null B null – Ellipse C. Unknown until code is actually run. – Square Square D. No output due to a syntax error. � Possible hierarchy E. No output due to a runtime error. p ClosedShape < Rectangle < Square ClosedShape <- Rectangle <- Square CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Inheritance and Polymorphism Computer Science Inheritance and Polymorphism
Recommend
More recommend