DOC PREVIEW
UMD CMSC 131 - Lecture Set #16: Inheritance

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture Set 16 Inheritance Inheritance Conceptual Is A relationship compared to contains a Terminology Overloading compared to Overriding super isInstanceOf and getClass CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr Inheritance A crucial feature of object oriented programming languages One class derived class subclass child class is constructed by including extending inheriting information from another base class superclass parent class and adding new information redefining existing Example Base class Clock setTime getTime tick Derived class Alarm Clock Same methods as Clock plus a few additional ones setAlarm ring CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 1 Can We Avoid Code Copying and therefore redundancy Alarm Clock IS A Clock Operations on Clock e g setTime should be inherited by Alarm Clock Alarm Clock should only have to add information specific to alarm clocks setAlarm ring Inheritance provides just this capability CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 2 1 Inheritance One class derived class subclass child class is constructed by including extending inheriting information from another base class superclass parent class then also adding new information and or redefining existing information To derive a class D from a base class B use Example public class D extends B Base class public class Shape Derived class public class Circle extends Shape Derived class inherits all instance variables methods from base class It can also define new instance variables methods Polymorphism object in derived class can be used anywhere base class is expected an alarmClock is a Clock CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 3 Inheritance vs Composition Inheritance a way to build new classes out of old ones Objects in subclass inherit data methods from superclass Object in a subclass is a n object in superclass Association another way to build new classes out of old Class definitions may include instance variables which are objects of other class types Object in a new class has a n object in the original class Composition the strongest form of association when the lifetime of the enclosed object is completely dependant on the lifetime of the object that contains it CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 4 Implements vs Extends When Defining a Class implements Keyword followed by the name of an interface Interfaces only have method prototypes Can t create on object of an interface type Can have a reference of the interface type point to an object of the class that implements it extends Keyword followed by the name of a class That class contains full method definitions Can create objects of that base class type Can have reference of the base class type point to an object of the class that extends it CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 5 2 Inheritance More Generally Classes objects have a natural is a hierarchy Object oriented programming provides mechanisms for exploiting this for Code re use Polymorphism Common operations implemented in super classes Objects in subclasses can be used wherever superclass objects are needed Shape Circle Triangle Right Triangle Animal Rectangle Insect Mammal Cat Primate Dog Human Ape Homer Equilateral Triangle Reptile CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 6 Example People at University Base class person Derived classes student faculty administrator Derived from those undergrad grad instructor professor Person Student Undergrad GradStudent Faculty Instructor Professor Administrator CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 7 University Person Example extends Person class Student instance variables int admitYear double gpa methods Student various int getAdmitYear double getGpa void setAdmitYear int void setGpa double String toString boolean equals Student class Person instance variables String name String idNum methods Person various String getName String getIdNum void setName String void setIdNum String String toString boolean equals Person CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr extends Person class Faculty instance variables int hireYear methods Faculty various int hireYear void setHireYear int String toString boolean equals Faculty 8 3 Method Overriding A derived class can define new instance variables and methods e g hireYear and getHireYear A derived class can also redefine override existing methods public class Person public String toString public class Student extends Person public String toString Overrides base class definition of this method Student bob new Student Bob Goodstudent 123 45 6789 2004 4 0 System out println Bob s info bob toString CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr Since bob is Student Student toString used 9 Overriding vs Overloading Overriding a derived class defines a method with same name parameters as base class Overloading two or more methods have the same name but different parameters Example Base class setName public class Person public void setName String n name n public class Faculty extends Person public void setName String n Overriding super setName The Evil Professor n public void setName String first String last Overloading super setName first last CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 10 Early vs Late Binding Consider Faculty carol new Faculty Carol Tuffteacher 999 99 9999 1995 Person p carol System out println p toString Which version of toString Person or Faculty is called Early static binding p is declared to be of type Person Therefore the Person version of toString is used Late dynamic binding The object to which p refers was created as Faculty object Therefore the Faculty version of toString is used Java uses late binding C by default uses early binding Early binding is more runtime efficient decisions about method versions can be made at compile time Late binding respects encapsulation object defines its operations when it is created CMSC 131 Fall 2009 Jan Plane adapted from Bonnie Dorr 11 4 Polymorphism Java s late binding makes it possible for a single reference variable to refer to objects of many different types Such a variable is said to be polymorphic meaning having many forms Example Create an array of various university people and print Person list new Person 3 list 0 new Person Col Mustard 000 00 0000 list 1 new Student Ms Scarlet 111 11 1111 1998 3 2 list 2 new Faculty Prof Plum 222 22 2222 1981 for int i 0 i list length i System out println list i


View Full Document

UMD CMSC 131 - Lecture Set #16: Inheritance

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set #16: Inheritance
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 Lecture Set #16: Inheritance 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 Lecture Set #16: Inheritance 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?