Unformatted text preview:

Topic6Topic 6Inheritance andInheritance and Polymorphism"Question: What is the object oriented way of getting rich?Answer: Inheritance.““Inheritance is new code that reuses old codeInheritance is new code that reuses old code.Polymorphism is old code that reuses new code.”CS 307 Fundamentals of Computer Science Inheritance and Polymorphism1Outline88Explanation of inheritance.8Using inheritance to create a SortedIntList.8Explanation of polymorphism.8Using polymorphism to make a more genericUsing polymorphism to make a more generic List class.CS 307 Fundamentals of Computer Science Inheritance and Polymorphism2Explanation of InheritanceExplanation of InheritanceCS 307 Fundamentals of Computer Science Inheritance and Polymorphism3Main Tenets of OO Programming8Encapsulation–abstraction, information hidingabs ac o , o a o d g8Inheritancecode reuse specialization"New code using old–code reuse, specialization New code using old code."8Polymorphism8Polymorphism– do X for a collection of various types of objects, where X isdifferentdepending on the type ofwhere X is differentdepending on the type of object–"Old code using new code"CS 307 Fundamentals of Computer Science Inheritance and Polymorphism4Old code using new code.Things and Relationships8Objtitd i ldt8Object oriented programming leads to programs that are modelsti d l f thi i th l ld–sometimes models of things in the real world– sometimes models of contrived or imaginary things8There are many types of relationships between8There are many types of relationships between the things in the modelschess piece has a position–chess piece has a position– chess piece has a colorchess piece moves (changes position)–chess piece moves (changes position)– chess piece is taken–arookisatypeofchesspieceCS 307 Fundamentals of Computer Science Inheritance and Polymorphism5a rook is a type of chess pieceThe “has-A” Relationship88Objects are often made up of many parts or have sub data.– chess piece: position, color– die: result, number of sides8This “has-a” relationship is modeled by compositionp– the instance variables or fields internal to objects8Encapsulation captures this conceptEncapsulation captures this conceptCS 307 Fundamentals of Computer Science Inheritance and Polymorphism6The “is-a” relationship88Another 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 student8“is-a”usually denotes some form ofisa usually denotes some form of specialization8it is not the same as“has-a”CS 307 Fundamentals of Computer Science Inheritance and Polymorphism7it is not the same as has-aInheritance88The “is-a” relationship, and the specialization that accompanies it, is modeled in object oriented languages via inheritance8Classes 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 Polymorphism8Nomenclature of Inheritance8IJ thtdkdidith8In Java the extendskeyword is used in the class header to specify which preexisting class a new class is inheriting fromae casss e gopublic class Student extends Person8 Person is said to be hlfSd–the parent class of Student– the super class of Student– the base class of Student–an ancestor of Student8 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 Polymorphism9Results of Inheritancepublic class Apublic class B extends A8the 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 supeclass, automatically8additional methods can be added to class Badditional methods can be added to class B (specialization)8the sub class can replace (redefine8the sub class can replace (redefine, override) methods from the super classCS 307 Fundamentals of Computer Science Inheritance and Polymorphism10Attendance Question 1What is the primary reason for using inheritance when programming?A. To make a program more complicatedB. To duplicate code between classesC Toreusepre-existing codeC. To reuse pre-existing codeD. To hide implementation details of a classEE. To ensure pre conditions of methods are met.CS 307 Fundamentals of Computer Science Inheritance and Polymorphism11Inheritance in Java8Java is a pure object oriented languageJava is a pure object oriented language8 all code is part of some class8all classes except one must inherit from8all classes, except one, must inherit from exactly one other class8TheObjectclass is thecosmic super classThe Objectclass 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, getClass8 implications:all classes are descendants ofObject–all classes are descendants of Object– all classes and thus all objects have a toString, equals, hashCode, clone, and getClass methodCS 307 Fundamentals of Computer Science Inheritance and Polymorphism12• toString, equals, hashCode, clone normally overriddenInheritance in Java8If l h d d t i l d th8If a class header does not include the extends clause the class extends the Obj tclass by defaultObjectclass by defaultpublic class Dieilll– Objectis an ancestor to all classes– it is the only class that does not extend some th lother class8A class extends exactly one other class– extending two or more classes is multiple inheritance. Java does not support this directly, th itIt fCS 307 Fundamentals of Computer Science Inheritance and Polymorphism13rather it uses Interfaces.Overriding methods8any method that is not final may be overridden by a descendant classy8same signature as method in ancestor8may not reduce visibility8may not reduce visibility8may use the original method if simply want to dd b h i t i tiadd more behavior to existingCS 307 Fundamentals of Computer Science Inheritance and Polymorphism14Attendance 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. 0BnullB. nullC. Unknown until code is actually run.D. No output due to a syntax error.E. No output due to a runtime error.CS 307


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?