1 / 18

Inheritance in Java: Deriving New Classes and Supporting Software Reuse

Learn how to derive new classes from existing ones, modify methods in child classes, and create well-designed class hierarchies using inheritance in Java. Explore its use in Java graphics frame.

rherrick
Download Presentation

Inheritance in Java: Deriving New Classes and Supporting Software Reuse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12 Inheritance

  2. Inheritance • Objectives • Derive new classes from existing ones • How does inheritance supports software reuse • How to add and modify methods in child classes • Extend simple class derivations into well-designed class hierarchies • Discuss its use in java graphics frame

  3. Introduction • Inheritance is the act of deriving a new class from an existing one • Derived class is a class created from an existing one • Eg: SavingsAccount is the derived class of Account class • The key idea here is the software reuse • Inheritance is the “is-a” relationship • Eg: Spinach “is-a” vegetable

  4. Terminology • The original class is called • Parent class or • Superclass or • Base class • The derived class is called • Child class or • subclass

  5. An Example - the base class book class Book { protected int pages = 1500; //---------------------------------------------------------------- // Prints a message concerning the pages of this book. //---------------------------------------------------------------- public void pageMessage () { System.out.println ("Number of pages: " + pages); } }

  6. The derived class dictonary class Dictionary extends Book { private int definitions = 52500; //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public void definitionMessage () { System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages); } }

  7. BOOK Dictionary

  8. The Protected Modifier • Not all variables and methods are inherited in derivation • The visibility modifier (public, protected, private) decides which gets inherited • Protected variables retains the encapsulation properties but derived class will inherit it

  9. The super reference • Constructors in base class are not inherited by the derived class, even if they have the public visibility • super is a general reference to a parent class • The parents constructor can be invoked using the super reference • It the super reference is included in the child’s constructor it must be the first line of the constructor

  10. Example - book2 class Book2 { protected int pages; //---------------------------------------------------------------- // Sets up the book with the specified number of pages. //---------------------------------------------------------------- public Book2 (int pages) { this.pages = pages; } //---------------------------------------------------------------- // Prints a message concerning the pages of this book. //---------------------------------------------------------------- public void pageMessage () { System.out.println ("Number of pages: " + pages); } }

  11. Example - Dictionary 2 class Dictionary2 extends Book2 { private int definitions; //----------------------------------------------------------------- // Sets up the dictionary with the specified number of pages // (maintained by the Book parent class) and defintions. //----------------------------------------------------------------- public Dictionary2 (int pages, int definitions) { super (pages); this.definitions = definitions; } //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public void definitionMessage () { System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages); } }

  12. Multiple inheritance • Java’s approach to inheritance is called “single inheritance” • Languages like C++ allow multiple inheritance but Java does not • PickUpTruck “is-a” car and a truck • If parents classes have similar methods, which ones the child supports? • However the use of interface provides some of the abilities of multiple inheritance • A java class can implement many interfaces • It allows to interact with a particular class while inheriting most crucial information

  13. Overriding methods • If a child class defines a method with the same name and signature, the child class overrides the parents method from the parent • Eg: Message class contains a method that instantiate two objects • One from Thought class • One from Advice class

  14. Examples class Messages { //----------------------------------------------------------------- // Instatiates two objects a invokes the message method in each. //----------------------------------------------------------------- public static void main (String[] args) { Thought parked = new Thought(); Advice dates = new Advice(); parked.message (); dates.message (); // overridden } }

  15. Class hierarchies • Multiple classes can be derived from a single parent. • Eg: animal class (parent), Rabbit, Cow, Cat (children) • Two children of the same parent are called siblings • Common features should be kept as high as possible in the class hierarchy • Inheritance mechanism is transitive • There are no single best hierarchy organization for all situations

  16. The object class • In Java all classes are ultimately derived from the “object” class. • If the class definition doesn’t uses the extends clause, then it is automatically derived from the object class. • class Thing{ } • class Thing extends Object{ } • Are equivalent

  17. Object class methods • boolean equals (Object obj); • Returns true if the objects are the same(aliases) • String toString(); • Returns a string representation of this object • Object clone(); • Creates and returns a copy of this object

  18. Abstract Classes • An abstract class is a generic concept in a class hierarchy • Abstract class cannot be instantiated • Abstract class is similar to an interface • Any class that contains one or more abstract methods is declared to be abstract • Eg: public abstract class StaffMember{} • Eg: Vehicle (abstract), derive car, Van,….

More Related