Inheritance 1
Inheritance 1
Inheritance is the capability of one class to inherit the properties from another class .A class from which
another class is inheriting the properties is Base/ Super /Parent class and the class that inherits the properties
is known as Derived / Sub /Child class.
Need of Inheritance/Advantages of Inheritance / Why was the concept of inheritance introduced in object
oriented languages?
2. Inheritance facilitates code reusability. Inheritance allows the addition of additional features by deriving a
class and adding new features to it.
3. Inheritance has transitive nature. For eg if class B is derived from class A and class C is derived from class B
then C will inherit the property of A
Forms of Inheritance
Single Inheritance : When a sub class inherits only from one base class , it is known as single inheritance.
y
Sub/ Derived /child class
Multiple Inheritance : When a sub class inherits properties from multiple base classes , it is known as Multiple
inheritance.[Java does not allow Multiple Inheritance].
Base Classes
X y
z
Sub class
Hierarchical Inheritance : When many subclasses inherits only from a single base
classes , it is known as Hierarchical inheritance
w Base Class
x z
y
Sub classes
Class
Multilevel Inheritance: Transitive nature of inheritance is reflected by this
form of inheritance. When a subclass inherits from class that itself inherits
from another class is Multilevel Inheritance.
X Base Class of Y
Y Sub Class of X
Base Class of Z
Z
Sub Class of Y
Hybrid Inheritance : When a sub class inherits from multiple base classes and all
of its base classes inherit from a single base class ,this form of inheritance is known
as Hybrid Inheritance
U V
W
W
X Y
X y
Function Overloading: Function overloading refers to a class having more than one function with
same name but different function signature(ie different number or types of arguments) is known as
function overloading.
For eg, Following code overloads a function area to compute areas of circle & rectangle.
When a method in a subclass has the same name and same function signature as a method in base
class, then the method in the subclass will override the same method of the base class.
When an overridden method is called within a sub class it will refer to the method defined in sub
class.
To refer to the overridden method keyword super is used. For eg super.overriddenMethod(). Now
this will refer to the method of the base class
class Parent
{
void show()
{
System.out.println("Parent's show()");
}
}
{ // super.show( ) is used to refer to show() of base class i. e To refer to the overridden method
System.out.println("Child's show()");
}
}
Abstract Class : is one that simply represents a concept and whose object cannot be created. It is
created through the use of keyword abstract.
Eg
public abstract class XYZ
{
…….
…….
}
Abstract Method: are the methods that are declared without any body in an abstract class is known
as abstract method. The method body will be defined in its sub class.
Eg public abstract void display();
Interface : Java does not allow multiple inheritance. To tie elements of different classes together
java uses interface
Syntax of Interface
interface interface_name
{ An interface defines a protocol of behavior .
……. The aim of the interfaces is to dictate common
Body of interface behavior among objects from different classes
}
(i) It begins with keyword interface
(ii) The interface body can declare two things
(a)Abstract Methods : Methods with empty body and should be public
(b)Constants: variables which cannot be modified they have to be declared with public
static final.
Implementation of Interfaces
A class can implement one or more interfaces using keyword implements in the class
followed by comma separated list of interfaces.
Eg
class classname implements interface1,interface2,…..
{
Interface body
}
A class can implement many interfaces but can extend only one class
Class P
interface X
interface Y
class P implements X ,Y
ISC 2020
An interface Data is defined with a data member and a method volume( ) which returns the
volume of the implementing shape. A super class Base has been defined to contain the radius of
a geometrical shape. Define a sub class CalVol which uses the properties of the interface Data
and the class Base and calculates the volume of a cylinder.
The details of the members of the interface and both the classes are given below:
Interface name Data
Data member:
double pi initialize pi = 3.142
Member functions/methods:
double volume( )
Class name: Base
Data member/instance
variable:
rad to store the radius in decimal
Member functions/methods:
Base(…) parameterized constructor to initialize the data member
void show( ) displays the radius with an appropriate message
Class name: CalVol
Data member/instance
variable:
ht to store the height in decimal
Member functions/methods:
CalVol(…) parameterized constructor to initialize the data members of both the classes
double volume( ) calculates the volume of a sphere by using the formula ( pi x radius^2 x height
)
void show( ) displays the data members of both the classes and the volume of the sphere
with appropriate message
Assume that the interface Data and the super class Base has been defined. Using the concept of
inheritance, specify the class CalVol giving the details of the constructor(…), double volume( ) and
void show( ).
The interface, super class, main function, and algorithm need NOT be written.