Lecture-12-Inheritance

Download Report

Transcript Lecture-12-Inheritance

Lecture 12
Inheritance
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
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
Terminology

The original class is called
–
–
–

Parent class or
Superclass or
Base class
The derived class is called
–
–
Child class or
subclass
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);
}
}
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);
}
}
BOOK
Dictionary
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
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
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);
}
}
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);
}
}
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
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
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
}
}
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
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
Object class methods

boolean equals (Object obj);
–

String toString();
–

Returns true if the objects are the same(aliases)
Returns a string representation of this object
Object clone();
–
Creates and returns a copy of this object
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,….