03-inheretance
03-inheretance
61
Inheritance
• On the surface, inheritance is a code re-use issue.
– we can extend code that is already written in a
manageable manner.
• Inheritance is more, it supports polymorphism at the
language level!
• Information is made manageable in a hierarchical
order
63
Inheritance cont.
• Take an existing object type (collection of fields and
methods) and extend it.
– create a special version of the code without re-
writing any of the existing code (or even explicitly
calling it!).
– End result is a more specific object type, called
the sub-class / derived class / child class.
– The original code is called the super class /
parent class / base class.
FulltimeEmployee
------------------------ University
Salary
Office Student
Contractor
Manager ------------------
------------------ HourlyRate,
CompanyCar ContractDuration
Email
Phone
66
Inheritance
public class Animal
{ Animal
Dog
67
Introduction (Cont.)
Animal
Private
Animal
Public Dog
Dog
Public
68
protected Members
• protected access
– Intermediate level of protection between public
and private
– protected members accessible by
• superclass members
• subclass members
• Class members in the same package
– Subclass access to superclass member
• Keyword super and a dot (.)
69
Introduction (Cont.)
Animal
Private
Protected
Animal Dog
Public Protected
Dog
Public
70
Example
71
IS-A Relationship (Example)
public class Animal
{ Animal
} extends
72
IS-A Relationship (Example)
public class Dog extends Mammal
{ Animal
public static void main(String args[])
{
Animal a = new Animal();
Reptile r = new Reptile(); Mamal Dog Reptile
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
73
IS-A Relationship
• In Object Oriented terms following are true:
– Animal is the superclass of Mammal class.
– Animal is the superclass of Reptile class.
– Mammal and Reptile are sub classes of Animal
class.
– Dog is the subclass of both Mammal and Animal
classes.
74
IS-A Relationship
• IS-A relationship we can say:
– Mammal IS-A Animal
– Reptile IS-A Animal
– Dog IS-A Mammal
– Hence : Dog IS-A Animal as well
• Use of the extends keyword the subclasses will be
able to inherit all the properties of the superclass
except for the private properties of the superclass
75
Accessing super class methods
• Can use super() to access all (non-private) super
class methods.
– even those replaced with new versions in the
derived class.
• Can use super() to call base class constructor.
FulltimeEmployee
------------------------
Salary
Office
Contractor
Manager ------------------
------------------ HourlyRate,
CompanyCar ContractDuration
Email
Phone
79
Inheritance Example
Employee
------------------
Name
Is a Email Is a
Phone
FulltimeEmployee
------------------------
Salary
Office
Is a Contractor
Manager ------------------
------------------ HourlyRate,
CompanyCar ContractDuration
Email
Phone
80
Example