Unformatted text preview:

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 Polymorphism2OutlineExplanation 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 ProgrammingEncapsulation–abstraction, information hidingInheritance–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 RelationshipsObject oriented programming leads to programs that are models–sometimes models of things in the real world–sometimes models of contrived or imaginary thingsThere 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” RelationshipObjects are often made up of many parts or have sub data.–chess piece: position, color–die: result, number of sidesThis “has-a” relationship is modeled by composition–the instance variables or fields internal to objectsEncapsulation captures this conceptCS 307 Fundamentals of Computer Science Inheritance and Polymorphism7The “is-a” relationship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“is-a” usually denotes some form of specializationit is not the same as “has-a”CS 307 Fundamentals of Computer Science Inheritance and Polymorphism8InheritanceThe “is-a” relationship, and the specialization that accompanies it, is modeled in object oriented languages via inheritance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 Computer Science Inheritance and Polymorphism9Nomenclature of InheritanceIn Java the extends keyword is used in the class header to specify which preexisting class a new class is inheriting frompublic class Student extends PersonPerson is said to be –the parent class of Student–the super class of Student–the base class of Student–an ancestor of StudentStudent 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 Athe sub class inherits (gains) all instance variables and instance methods of the super class, automaticallyadditional 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 JavaJava is a pure object oriented languageall code is part of some classall classes, except one, must inherit from exactly one other 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implications:–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 JavaIf 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 class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.CS 307 Fundamentals of Computer Science Inheritance and Polymorphism14Overriding methodsany method that is not final may be overridden by a descendant classsame signature as method in ancestormay not reduce visibilitymay 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

UT CS 307 - Inheritance and Polymorphism

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Inheritance and 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 and 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 and 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?