Inheritance: The literal meaning of the
term inheritance is to receive something by succession. This is true
for the inheritance feature of java as well. It means that one class,
known as a child class or sub-class, acquires the behaviors and
properties of another class, which is called the parent class or
super class.
Inheritance is useful for
two main reasons: code re-usability and method overriding. When a
class inherits the properties of another class, then it acquires its
fields and methods, and there is also the option of adding new
properties. Therefore we do not need to write the code again and
again. Method overriding is a feature that allows us to override the
method of the main class with that of a sub-class. This is useful for
polymorphism.
The syntax for inheritance
is simple. The “extends” keyword is used to show that one class
will inherit the properties of the class that comes after the
“extends” keyword. This is an example:
class sub-class extends
Super class
{
method;
}
Types of Inheritance in
Java:
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple Inheritance
- Hybrid Inheritance
This type of inheritance
refers to the extension of one class by another. So there is one
parent class and one child class. In the above image, class B extends
class A.
Example:
Class A
{
void method()
{
System.out.println("Hello");
}
}
Class B extends A
{
void show()
{
System.out.println("Hello B");
}
public static void main(String args[])
{
B obj = new B();
obj.method();
obj.show();
}
}
{
void method()
{
System.out.println("Hello");
}
}
Class B extends A
{
void show()
{
System.out.println("Hello B");
}
public static void main(String args[])
{
B obj = new B();
obj.method();
obj.show();
}
}
output:
Hello
Hello B
Multilevel inheritance
Multilevel inheritance is a chain of
inheritance. As in the image, class C inherits from class B while
class B inherits from class A.
Example:
Class A
{
void methodA()
{
System.out.println("Hello A");
}
}
Class B extends A
{
void methodB()
{
System.out.println("Hello B");
}
}
Class C extends B
{
void methodC()
{
System.out.println("Hello C");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA();
obj.methodB();
obj.methodC();
}
}
{
void methodA()
{
System.out.println("Hello A");
}
}
Class B extends A
{
void methodB()
{
System.out.println("Hello B");
}
}
Class C extends B
{
void methodC()
{
System.out.println("Hello C");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA();
obj.methodB();
obj.methodC();
}
}
Hierarchical inheritance
In Hierarchical inheritance, one class is the parent class while there are two or
more child classes. For example, in the above image, class B and class C
both extend class A.
Class A
{
void methodA()
{
System.out.println("Hello A");
}
}
Class B extends A
{
void methodB()
{
System.out.println("Hello B");
}
}
Class C extends A
{
void methodC()
{
System.out.println("Hello C");
}
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
obj1.methodA();
obj1.methodB();
obj2.methodC();
}
}
Output:
Hello A
Hello B
Hello C
{
void methodA()
{
System.out.println("Hello A");
}
}
Class B extends A
{
void methodB()
{
System.out.println("Hello B");
}
}
Class C extends A
{
void methodC()
{
System.out.println("Hello C");
}
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
obj1.methodA();
obj1.methodB();
obj2.methodC();
}
}
Output:
Hello A
Hello B
Hello C
Note : unfortunately Java doesn't support Multiple Inheritance and Hybrid Inheritance.
Complete core java course
at ADMEC Multimedia Institute in both classroom and online mode.
ADMEC in a ISO Certified software training institute which offers C
Language training, C plus plus language training and many more....
No comments:
Post a Comment