0% found this document useful (0 votes)
4 views

Inheritance

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Inheritance

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

inheritance

• Inheritance is one of the key features of Object Oriented


Programming.
• Inheritance can be defined as the process where one class
acquires the properties (methods and fields) of another.
• The class which inherits the properties of other is known
as subclass (derived class, child class) and the class
whose properties are inherited is known as superclass
(base class, parent class).
• extends Keyword
• extends is the keyword used to inherit the properties of a
class.
• Following is the syntax of extends keyword.
• Syntax:
class Super
{
.....
.....
}
class Sub extends Super
{
.....
.....
}
Types of inheritance
• Single Inheritance.
• Multiple Inheritance (Through Interface)
• Multilevel Inheritance.
• Hierarchical Inheritance.
• Hybrid Inheritance (Through Interface)
1. Single inheritance
• Derivation of a class from only one base class is called single
inheritance.

X is a base class. Y is a derived class. This type


X
involves one base class and derived class. Further,
no class is derived from Y
Y

• Further derived class is not used as a base class.


• This type of inheritance use only one base class and one derived
class.
• A derived class inherits data member variables and functions of
base class. i.e.,
• The newly created class receives entire characteristics from its
Eg: of single inheritance (file name : B.java)
class A
{
public int x;
}
class B extends A
{
public int y;
public static void main(String[] args)
{
B obj = new B();
obj.x = 20;
obj.y = 30;
System.out.println("the value of A : "+obj.x);
System.out.println("the value of A : "+obj.y);
}
2. Multilevel inheritance
• The procedure of deriving a class from another derived class is
called multilevel inheritance.

X X is a base class. Y is derived from X. further


, Z is derived from Y. Here, Y is not only a
derived class but also a base class. Further z
Y
can be as a base class.

Z
Eg: of multilevel inheritance (file name : C.java)
class A
{
public int x;
}
class B extends A
{
public int y;
}
class C extends B
{
public int z;
public static void main(String[] args)
{
C obj = new C();
obj.x = 20;
obj.y = 30;
obj.z = 40;
System.out.println("the value of A : "+obj.x);
System.out.println("the value of A : "+obj.y);
System.out.println("the value of A : "+obj.z);
}
}
3. Hierarchical inheritance
• When a single base class is used for derivation of two or more
classes, it is known as hierarchical inheritance.

W
W is only one base class. X, Y and Z are
derived classes. Further, X, Y and Z are
not used for deriving a class.
X Y Z

• In hierarchical arrangement of classes, derived classes share


the properties of base class.
• Hierarchical unit shows top down style through splitting a
compound class into several simple sub classes.
Eg: hierarchical inheritance
class A
{
public int x;
public int y;
}
class B extends A
{
public int z;
public void sum()
{
z = x+y;
System.out.println("the result of sum is : "+z);
}
}
class C extends A
{
public int u;
public void sub()
{
u = x-y;
System.out.println("the result of subtraction is : "+u);
}
public static void main(String[] args)
{
B ob1 = new B();
ob1.x = 60;
ob1.y = 50;
ob1.sum();

C ob2 = new C();


ob2.x = 30;
ob2.y = 20;
ob2.sub();
Method overriding(Runtime Polymorphism)
• Polymorphism:
• Means one operation can be implemented in different
ways in different classes.
• Method overriding:
• Child class can redefine methods of the parent class. This
is called method overriding. The signature (return type,
parameter type and number of parameters) is kept same
as defined in the parent class.
• Method overriding is done to achieve Run Time
Polymorphism
Eg: of method overriding
class Vehicle
{
void run( )
{
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle
{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Vehicle v = new Vehicle();
v.run(); o/p
Vehicle is running
Bike b = new Bike(); Bike is running safely
b.run();
}
}
• However, in the runtime, JVM figures out the object type and
would run the method that belongs to that particular object.

You might also like