DOC PREVIEW
UMD CMSC 131 - Lecture Set #19: 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 19 Inheritance Inheritance Conceptual Is A relationship compared to contains a Terminology Overloading compared to Overriding super isInstanceOf and getClass CMSC 131 Sping 2008 Jan Plane adapted from Bonnie Dorr Inheritance A crucial feature of object oriented programming languages One class derived class subclass 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 Spring 2008 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 Spring 2008 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 public class D extends B Example we will look at this in next two slides 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 Spring 2008 Jan Plane adapted from Bonnie Dorr 3 Inheritance More Generally Classes objects have a natural is a hierarchy Object oriented programming provides mechanisms for exploiting this for Code re use Common operations implemented in super classes Polymorphism 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 Spring 2008 Jan Plane adapted from Bonnie Dorr 4 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 CMSC 131 Spring 2008 Jan Plane adapted from Bonnie Dorr Administrator 5 2 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 extends Person class Faculty instance variables int hireYear methods Faculty various int hireYear void setHireYear int String toString boolean equals Faculty CMSC 131 Spring 2008 Jan Plane adapted from Bonnie Dorr 6 Memory Layout and Initialization Order When you create a new derived class object Java allocates space for base class instance variables and derived class variables Java initializes base class variables first and then the derived class variables Example Person ted new Person Ted Goodman 111 22 3333 Student carole new Student Carole Goode 123 45 6789 2004 4 0 Heap Ted Goodman ted 111 22 3333 carole Carole Good 2004 4 0 123 45 6789 super n id builds the Person part Student constructor finishes it off CMSC 131 Spring 2008 Jan Plane adapted from Bonnie Dorr 7 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 Spring 2008 Jan Plane adapted from Bonnie Dorr Since bob is Student Student toString used 8 3 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 Spring 2008 Jan Plane adapted from Bonnie Dorr 9 Calling an overridden function Possible but use sparingly Overriding hides methods of the base class can still access them using super methodName in subclass but not in outside world public class Person public String toString one def here public class Administrator extends Person public String toString different def here public String regPrint return super toString will use Person s def of toString return toString will use Administrator s def of toString Often better to pick a different name rather than overload if you want both CMSC 131 Spring 2008 Jan Plane adapted from Bonnie Dorr 10 Derived class Student package university public class Student extends Person private int admitYear Additional private double gpa public Student super admitYear 1 gpa 0 0 Tells Java that Student is derived from Person instance variables Default constructor This calls the default constructor for base class superclass Person to set name and idNum public Student String n String id int yr double g Standard constructor super n id admitYear yr Calls Person constructor gpa g public Student Student s Copy constructor super s admitYear s admitYear Calls Person copy constructor gpa s gpa other methods in part 2 CMSC 131 Spring 2008 Jan Plane adapted from Bonnie Dorr 11 4 Understanding the Student extends specifies that Student is subclass of Person public class Student extends Person super When creating a new Student object we need to initialize its base class instance variables from Person This is done by calling super E g super name id invokes constructor Person name id super must be the first statement of your constructor If you do not call super Java will automatically invoke the base class s default constructor If the base class s default constructor is


View Full Document

UMD CMSC 131 - Lecture Set #19: 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 #19: 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 #19: 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 #19: 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?