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
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
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
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
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
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:

1 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 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) 1 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) 2 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 capability2 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 3 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:  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) 4 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) 5 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 it3 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 6 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 Rectangle Triangle Right-Triangle Equilateral-Triangle Animal Insect Reptile Mammal Cat Dog Primate Human Ape Homer CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 7 Example: People at University  Base class: person  Derived classes: student, faculty, administrator  Derived from those: undergrad, grad, instructor, professor,… Person Student Faculty Administrator Undergrad GradStudent Instructor Professor … … Person Student Faculty CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 8 University Person Example 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 ) 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 ) extends Person class: Faculty instance variables: int hireYear methods: Faculty( … ) [various] int hireYear( ) void setHireYear( int ) String toString( ) boolean equals( Faculty ) extends Person4 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 9 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( ) { … } } Student bob = new Student("Bob Goodstudent","123-45-6789",2004,4.0 ); System.out.println( "Bob's info: " + bob.toString( ) ); Overrides base-class definition of this method Since bob is Student, Student toString used CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 10 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 public class Person { public void setName( String n ) { name = n; } … } public class Faculty extends Person { public void setName( String n ) { super.setName( “The Evil Professor ” + n ); } public void setName( String first, String last ) { super.setName( first + “ ” + last ); } } Base class setName( ) Overriding Overloading CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 11 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


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 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?