17InheritanceSmall

17InheritanceSmall - Lecture Set #17: Inheritance...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) Lecture Set #17: Inheritance Inheritance c Conceptual c Is-A relationship compared to contains-a c Terminology c Overloading compared to Overriding c super c isInstanceOf and getClass() CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 1 Inheritance c A crucial feature of object-oriented programming languages c One class ( derived class , subclass, child class ) is constructed … c … by including ( extending, inheriting ) information … c … from another ( base class , superclass , parent class ) … c … and adding new information / redefining existing c Example c Base class: Clock c setTime c getTime c tick c Derived class: Alarm Clock c Same methods as Clock plus a few additional ones: setAlarm, ring CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 2 Can We Avoid Code Copying and therefore redundancy? c Alarm Clock “IS-A” Clock c Operations on Clock (e.g. setTime ) should be inherited by Alarm Clock c Alarm Clock should only have to add information specific to alarm clocks c setAlarm c ring c Inheritance provides just this capability
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 3 Inheritance c 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 c To derive a class D from a base class B, use: public class D extends B { … } c Example (we will look at this in next two slides): c Base class: public class Shape c Derived class: public class Circle extends Shape c Derived class inherits all instance variables, methods from base class. It can also define new instance variables, methods c Polymorphism : object in derived class can be used anywhere base class is expected (an alarmClock is a Clock !) CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 4 Inheritance vs. Composition c Inheritance: a way to build new classes out of old ones c Objects in subclass inherit data, methods from superclass c Object in a subclass “is-a”(n) object in superclass c Association: another way to build new classes out of old c Class definitions may include instance variables which are objects of other class types c Object in a new class “has-a”(n) object in the original class c 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 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 5 Implements vs. Extends When Defining a Class c implements: c Keyword followed by the name of an interface c Interfaces only have method prototypes c Can’t create on object of an interface type c Can have a reference of the interface type point to an object of the class that implements it c extends: c Keyword followed by the name of a class c That class contains full method definitions
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

17InheritanceSmall - Lecture Set #17: Inheritance...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online