Slide 1OutlineExplanation of InheritanceMain Tenets of OO ProgrammingThings and RelationshipsThe “has-A” RelationshipThe “is-a” relationshipInheritanceNomenclature of InheritanceResults of InheritanceAttendance Question 1Inheritance in JavaSlide 13Overriding methodsAttendance Question 2Shape ClassesA ClosedShape classConstructorsA Rectangle ConstructorA Rectangle ClassThe Keyword superInitialization methodResult of InheritanceThe Real PictureAccess Modifiers and InheritanceWhy private Vars and not protected?Required updateCreating a SortedIntListA New ClassImplementing SortedIntListOverriding the add MethodProblemsExplanation of PolymorphismPolymorphismData TypeAttendance Question 3What’s the Output?Method LookUpAttendance Question 4Why Bother?Genericitythe createASet examplecreateASet examplesA Generic List ClassBack to IntListGeneric List ClassWriting an equals Methodequals methodCS 307 Fundamentals of Computer Science Inheritance and Polymorphism1Topic 6Inheritance and Polymorphism"Question: What is the object oriented way of getting rich?Answer: Inheritance.““Inheritance is new code that reuses old code.Polymorphism is old code that reuses new code.”CS 307 Fundamentals of Computer Science Inheritance and Polymorphism2OutlineExplanation of inheritance.Using inheritance to create a SortedIntList.Explanation of polymorphism.Using polymorphism to make a more generic List class.CS 307 Fundamentals of Computer Science Inheritance and Polymorphism3Explanation of InheritanceCS 307 Fundamentals of Computer Science Inheritance and Polymorphism4Main Tenets of OO ProgrammingEncapsulation–abstraction, information hidingInheritance–code reuse, specialization "New code using old code."Polymorphism–do X for a collection of various types of objects, where X is different depending on the type of object–"Old code using new code."CS 307 Fundamentals of Computer Science Inheritance and Polymorphism5Things and RelationshipsObject oriented programming leads to programs that are models–sometimes models of things in the real world–sometimes models of contrived or imaginary thingsThere are many types of relationships between the things in the models–chess piece has a position–chess piece has a color–chess piece moves (changes position)–chess piece is taken–a rook is a type of chess pieceCS 307 Fundamentals of Computer Science Inheritance and Polymorphism6The “has-A” RelationshipObjects are often made up of many parts or have sub data.–chess piece: position, color–die: result, number of sidesThis “has-a” relationship is modeled by composition–the instance variables or fields internal to objectsEncapsulation captures this conceptCS 307 Fundamentals of Computer Science Inheritance and Polymorphism7The “is-a” relationshipAnother 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“is-a” usually denotes some form of specializationit is not the same as “has-a”CS 307 Fundamentals of Computer Science Inheritance and Polymorphism8InheritanceThe “is-a” relationship, and the specialization that accompanies it, is modeled in object oriented languages via inheritanceClasses 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 Computer Science Inheritance and Polymorphism9Nomenclature of InheritanceIn Java the extends keyword is used in the class header to specify which preexisting class a new class is inheriting frompublic class Student extends PersonPerson is said to be –the parent class of Student–the super class of Student–the base class of Student–an ancestor of StudentStudent is said to be–a child class of Person–a sub class of Person–a derived class of Person–a descendant of PersonCS 307 Fundamentals of Computer Science Inheritance and Polymorphism10Results of Inheritancepublic class Apublic class B extends Athe sub class inherits (gains) all instance variables and instance methods of the super class, automaticallyadditional methods can be added to class B (specialization)the sub class can replace (redefine, override) methods from the super classCS 307 Fundamentals of Computer Science Inheritance and Polymorphism11Attendance Question 1What is the primary reason for using inheritance when programming?A. To make a program more complicatedB. To duplicate code between classesC. To reuse pre-existing codeD. To hide implementation details of a classE. To ensure pre conditions of methods are met.CS 307 Fundamentals of Computer Science Inheritance and Polymorphism12Inheritance in JavaJava is a pure object oriented languageall code is part of some classall classes, except one, must inherit from exactly one other classThe 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, getClassimplications:–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 overriddenCS 307 Fundamentals of Computer Science Inheritance and Polymorphism13Inheritance in JavaIf a class header does not include the extends clause the class extends the Object class by defaultpublic class Die–Object is an ancestor to all classes–it is the only class that does not extend some other classA class extends exactly one other class–extending two or more classes is multiple inheritance. Java does not support this directly, rather it uses Interfaces.CS 307 Fundamentals of Computer Science Inheritance and Polymorphism14Overriding methodsany method that is not final may be overridden by a descendant classsame signature as method in ancestormay not reduce visibilitymay use the original method if simply want to add more behavior to existingCS 307 Fundamentals of Computer Science Inheritance and Polymorphism15Attendance Question 2What 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. nullC. Unknown until code is actually run.D. No
View Full Document