Inheritance Notes
Inheritance Notes
INHERITANCE
Concept
i) Inheritance is based on the idea of reusability.
ii) This means that the features of an existing class are reused and new features are added to it.
iii) The existing class undergoes no modifications (i.e. it remains unchanged), it is just extended to form a new
class.
Basics
Inheritance is the technique of creating a new class by building upon an existing class definition.The existing
class is referred to as parent or super or base class, and the new class is called a child or sub or derived class.
Types of Inheritance:
1. Single Inheritance:
B
X
2. Hierarchical Inheritance:
X Y Z
3. Multilevel Inheritance:
X
Z
The Object class:
The Object class defined in java.lang package specifies the topmost (i.e. root) class of the Java class hierarchy tree. In
the absence of any other explicit superclass, every class is implicitly a subclass of the Object class.
Note:-
i) If the superclass definition contains a parameterized constructor then, the subclass must have a parameterized
constructor explicitly invoking a super(…).
ii) Explicit invocation of a superclass constructor must be the first line in the subclass constructor.
Order of Invocation:
When a subclass is instantiated, the object will begin with an invocation of the constructor in the base class and initialize
downwards through constructors in each subclass till it reaches the final subclass constructor.
Method Overriding
It is a concept which occurs when an instance method in a subclass hides or overrides an inherited method of the
superclass due to similar declarations.
Overridden methods are methods that are redefined within an inherited class or subclass.
An overridden super class method can be invoked in a subclass using the keyword super as:
super . overriddenMethod( ) ;
Abstract Classes
i) An abstract class defines a structure of given abstraction without any implementation.
ii) In Java, an abstract class is a special type of superclass that is specified by using the abstract keyword as:
abstract class < classname >
{ ……. }
iii) Abstract classes cannot be instantiated, as they are defined solely for the purpose of extension (inheritance) by
other classes.
Abstract Methods
i) It is an overridden method which is declared with the abstract modifier in an abstract super class, but implemented
specifically in a subclass.
Declaration Syntax:
abstract type <methodname> ([ parameter–list ]) ;
Polymorphism: It is the capability of an action or method to do different things based on the object it is acting upon.
C o m p ile T im e R u n T im e
A ls o c a lle d e a r l y o r s t a t i c A ls o c a lle d la t e o r d y n a m ic
b in d in g . b in d in g .
I t is a c h ie v e d u s in g m e t h o d I t is a c h ie v e d u s in g m e t h o d
o v e r lo a d in g . o v e r r id in g .
I t is a m e c h a n is m b y w h ic h th e I t is a m e c h a n is m b y w h ic h th e
c a ll t o a n o v e r lo a d e d m e th o d c a ll to a n o v e r r id d e n m e th o d
is r e s o lv e d b y th e c o m p ile r a t is r e s o lv e d b y t h e J V M a t th e
th e c o m p ile tim e . r u n t im e .
// Dynamic binding using an overridden method
abstract class Shape { / / abstract super class
double ar ;
abstract void area( ) ; // abstract method declaration
}
class Circle extends Shape { // subclass
double radius ;
Circle(double r) {
radius = r ;
}
public void area( ) { // abstract method definition version 1
ar = 3.14 radius radius ;
(40)
System.out.println(“Circle area = “ +ar) ;
}
}
class Triangle extends Shape { // subclass
double base, height ;
Triangle(double b, double h) {
base = b ; height = h ;
}
public void area( ) { // abstract method definition version 2
ar = base height / 2 ;
System.out.println(“Triangle area = “ +ar) ;
}
}
class PolyTest {
public static void main(String args[ ]) throws Exception {
Shape s ; // abstract superclass reference variable
s = new Circle(2.0) ; // s refers to Circle object
s.area( ) ; // invokes area( ) of Circle
s = new Triangle(2.0, 5.0) ; // s refers to Triangle object
s.area( ) ; // invokes area( ) of Triangle
} // end of main
} // end of main class
Output: Circle area = 12.56
Triangle area = 5.0
The type of subclass object referred to by the superclass reference variable at run time dynamically determines the
execution of the correct version of the abstract method, thus implementing Java’s approach to subclass polymorphism.
Interface
i) An interface in Java is a collection of final constants and abstract methods.
ii) Interfaces are syntactically similar to classes, but they lack instance variables and their methods specify only
their signatures, not their code. For e.g.
interface Area { // An interface definition
double pi = 3.14 ;
double calcArea(double x, double y) ;
}
iii) The implements clause in a class definition implements the interface. For e.g.
// An interface implementation
class Rectangle implements Area {
public double calcArea(double x, double y) {
return (x y) ;
}
}
iv) Interfaces are used like a super class whose properties can be implemented by one or more classes.
Class Vs Interface:
i) A class consists of instance variables and methods definitions while an interface consists of final constants and
method declarations.
ii) A class can be instantiated using the new operator while an interface cannot be instantiated as they are abstract
in nature.
Abstract Class Vs Interface:
i) An abstract class is defined using abstract keyword while an interface is specified using keyword interface.
ii) An abstract class is implemented using extends keyword while an interface is implemented using keyword
implements.
(41)