Inheritance:
In java inheritanceis a mechanism which one object can get all the
properties and behaviors of a parent object. In inheritance there is
a relationship between two classes. That's why it is-a relationship.
For example: Bike, car, bus this are
vehicle. That's why the child of vehicle
are bike, car, bus and vehicle is the
parent of this objects.
Vehicle
Bike, car,
bus
3.
Java inheritance:
Theimportant of inheritance in java program is code
reusability. We can reuse the function of parent class at
child class.
In java we use Extends keyword for inheritance.
Subclass and Superclass in inheritance.
Java
Inheritance
Simple program
public classSuper {
public void display()
{
System.out.println("I am parent class");
}
}
class Sub extends Super {
public static void main (String args[])
{
Sub message = new Sub();
message.display();
}
} Output: I am parent class
Super
________________________________
I am from parent class
sub
Parent class
Child class
Java
Inheritance
6.
Types of Inheritance:
Single inheritance.
Multiple inheritance.
Multi-level inheritance.
Hierarchical inheritance.
Hybrid inheritance.
Note: In java Multiple inheritance is not supported.
7.
Single Inheritance:
Single inheritanceis the most easy type to understand. In single
inheritance a class extends another only one class. The child class
inherit only parents class. The subclass inherit the objects of
superclass.
Example: A cat is a animal.
So a cat only can inherit the
objects of animal class.
Java
Inheritance
Cat
(subclass)
Animal
(Superclass)
8.
Syntax of SingleInheritance:
class A
{
//code
}
class B extend A
{
//code
}
B
A
9.
Sample Program ofSingle Inheritance:
class message_super {
public void display()
{
System.out.println("I am from superclass");
}
}
class message_sub extends message_super {
public static void main (String args[])
{
message_sub message = new message_sub();
message.display();
}
}
Output: I am from superclass
Message_super
________________________________
I am from superclass
Message_sub
Java
Inheritance
10.
Multilevel Inheritance
When aclass extends a class, and that class extends another class
and its object then it call multilevel inheritance.
Example: a class A is parent class.
Class B is inherit the objects of class A. Another
one class c is child of class B.
class c can inherit the objects
of class C.
A
B
C
Base class
Intermediary
class
Child class
11.
Syntax of MultilevelInheritance:
class A {
//code
}
class B extend A {
//code
}
class C extend B {
//code
}
Java
Inheritance
B
A
C
12.
Sample Program ofMultilevel Inheritance:
class a {
int data = 15;
}
class b extends a {
}
class c extends b {
public void display() {
System.out.println("Number is:"+data);
}
public static void main (String args[])
{
c num = new c();
num.display();
}
}
Output: number is: 5
c
b
a
______________
Data = 5
13.
Hierarchical inheritance:
When onesuperclass can be inherited by more than one subclass
then it is called hierarchical inheritance.
Example: a class A is parent class.
Class B is inherit the
objects of class A. Another
one class c also can inherit the
objects of class A.
Java
Inheritance
C
B
A
14.
Syntax of Hierarchicalinheritance:
class Food {
//code
}
class Rice extends Food {
//code
}
class Fruits extends Food {
//code
}
Fruit
Rice
Food
15.
Sample Program ofHierarchical Inheritance:
class animal {
public void display() {
System.out.println("I am animal");
}
}
class tiger extends animal {
public void display() {
System.out.println("I am tiger");
}
public static void main(String args[]) {
tiger t = new tiger();
t.display();
}
}
Java
Inheritance
Output: I am tiger