You are on page 1of 11

INHERITANCE

Inheritance 18CS45: Object Oriented Concepts 1


Objectives
• Inheritance basics
• Importance of Inhertiance
• Using super
• creating multilevel hierarchy
• Method overriding

Inheritance 18CS45: Object Oriented Concepts 2


Inheritance Basics
• Inheritance allows a class to inherit the instance variables and methods defined
in another class.
• To inherit one class into another class we use a keyword called extends.
• A class that is Inherited is called super class
• The class that does the inheriting is called a sub class

• Example

Inheritance 18CS45: Object Oriented Concepts 3


Inheritance Basics (Cont…)
• A subclass includes all of the members of its super class but it cannot
access those members of the super class that have been declared as
private.

• Example

Inheritance 18CS45: Object Oriented Concepts 4


Importance of Inheritance
• Example

• Once we create a superclass that defines the attributes common to a


set of objects, it can be used to create any number of more specific
subclasses

Inheritance 18CS45: Object Oriented Concepts 5


A Super class Variable Can Reference a
Subclass Object

• Example

• It is the type of the reference variable — not the type of the object
that it refers to — that determines what members can be
accessed.

Inheritance 18CS45: Object Oriented Concepts 6


Using super
• super has two uses:
• Calls the superclass constructor.
• Is used to access a member of the superclass that has been hidden by a
member of a subclass.

Inheritance 18CS45: Object Oriented Concepts 7


Using super to Call Super class Constructors
• A subclass can call a constructor method defined by its superclass by use of the
following form of super:
super(parameter-list);

• super( ) must always be the first statement executed inside a subclass constructor.

• When a subclass calls super( ), it is calling the constructor of its immediate


superclass.

• Example

Inheritance 18CS45: Object Oriented Concepts 8


A Second Use for super
• The second form of super is most applicable in situations where member names
of a subclass hide members by the same name in the super class.

• The general form


super.member

• Example

Inheritance 18CS45: Object Oriented Concepts 9


Creating a Multilevel Hierarchy
• Example

• Note:
• A subclass can be a super class for another subclass
• Java does not support the inheritance of multiple super classes into a single
subclass.

Inheritance 18CS45: Object Oriented Concepts 10


Method Overriding
• When a method in a subclass has the same name and type signature as a method in its
super class, then the method in the subclass is said to override the method in the super
class.

• When an overridden method is called from within a subclass, it will always refer to the
version of that method defined by the subclass.

• Example

• Method overriding occurs only when the names and the type signatures of the two
methods are identical. If they are not, then the two methods are simply overloaded.

• Example
Inheritance 18CS45: Object Oriented Concepts 11

You might also like