19InheritanceLarge

19InheritanceLarge - Lecture Set #19: Inheritance...

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

View Full Document Right Arrow Icon
1 CMSC 131 Sping 2008 Jan Plane (adapted from Bonnie Dorr) Lecture Set #19: 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 Spring 2008 Jan Plane (adapted from Bonnie Dorr) 1 Inheritance c A crucial feature of object-oriented programming languages c One class ( derived class , subclass ) 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 CMSC 131 Spring 2008 Jan Plane (adapted from Bonnie Dorr) 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 CMSC 131 Spring 2008 Jan Plane (adapted from Bonnie Dorr) 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 !)
Background image of page 2
3 CMSC 131 Spring 2008 Jan Plane (adapted from Bonnie Dorr) 4 Inheritance More Generally c Classes / objects have a natural “is-a” hierarchy c Object-oriented programming provides mechanisms for exploiting this for c Code re-use Common operations implemented in super classes c 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 Spring 2008 Jan Plane (adapted from Bonnie Dorr) 5 Example: People at University c Base class: person c Derived classes: student, faculty, administrator c Derived from those: undergrad, grad, instructor, professor,… Person Student Faculty Administrator Undergrad GradStudent Instructor Professor
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 CMSC 131 Spring 2008 Jan Plane (adapted from Bonnie Dorr) 6 University Person Example class : Person instance variables : String name String idNum methods : Person( … ) [various] String getName( ) String getIdNum( ) void setName( String ) void setIdNum(
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

19InheritanceLarge - Lecture Set #19: Inheritance...

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

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