Inheritance in
JAVA
Inheritance is a process in which the child class ( also
called sub class) uses the features(methods, properties) of parent class (also
called super class). The child class uses the word “extends” to use the
features of the parent class. Hence we
can reuse the code, method of the parent class.
Types of
Inheritance:
Single level
Multilevel
Multiple
Hybrid
Hierarchical
Hierarchical
1 Single level Inheritance
In this, there is one parent class and one child class.
And child is inheriting the parent class properties/features. But parent class
can not inherit child class properties.
For example:
Create
a parent class "Parent"
public class Parent {
String text = "Hello";
public void methodP(){
System.out.println("This is Parent_Class .");
}
}
Create a child class Child
public class Child {
public void methodC(){
System.out.println("This is child_Class .");
}
}
To call and run the methods from Parent Class and the Child
Class, without using inheritance, we
need to create object for each of them :
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
System.out.println("The text in parent class is:"+p.text);
p.methodP();
c.methodC();
}
When we run this main method, it prints :
The text
in parent class is:Hello
This is
Parent_Class .
This is child_Class .
We get the same output by using inheritance as below and there is no need to create object for the parent class:
We use “extends” keyword in the child class to apply inheritance:
public class Child extends Parent {
public void methodC(){
System.out.println("This is child_Class .");
}
}
Now , creating a child class object will be enough to
call the parent class , there is no need to create object for the parent class:
public static void main(String[] args) {
Child c = new Child();
System.out.println("The text in parent class is:"+c.text);
c.methodP();
c.methodC();
}
Now, when we run the main method, we get below output:
The text
in parent class is:Hello
This is
Parent_Class .
This is child_Class .
Creating an object for a class , means allocating some
memory space for the class. Since, we can avoid creating object for the parent
class using inheritance, we can improve the programming performance.
Multilevel
Inheritance:
In single level inheritance, we saw that there are one
parent and one child. In multilevel inheritance there is one more level of
class called GrandParent. The clild class extends to parent class and the
parent class extends to the grandparent class. In this way, the clild class can
access the properties of parent class as well as the grand parent class even
though the child class does not have the direct relationship with the grand
parent class.
For example:
Create
a grand parent class:
public class GrandParent {
public void methodGP(){
System.out.println("This is Grand_Parent_Class .");
}
}
Create a parent class :
public class Parent {
String text = "Hello";
public void methodP(){
System.out.println("This is Parent_Class .");
}
}
Create a child class:
public class Child {
public void methodC(){
System.out.println("This is child_Class .");
}
}
Now, to call the child, parent and grand parent classes in
main method, we need to create object for each classes.
public static void main(String[] args) {
GrandParent g = new GrandParent();
Parent
p = new Parent();
Child c = new Child();
g.methodGP();
System.out.println("The text in parent class is:"+p.text);
p.methodP();
c.methodC();
}
When we run the main method, we get below output:
This is
Grand_Parent_Class .
The text
in parent class is:Hello
This is
Parent_Class .
This is
child_Class .
Now, by using multilevel inheritance we can avoid creating objects for the parent and grand
parent classes as below:
Let’s use inheritance in parent class
public class Parent extends GrandParent {
String text = "Hello";
public void methodP(){
System.out.println("This is Parent_Class .");
}
}
Let’s use inheritance in child class
public class Child extends Parent {
public void methodC(){
System.out.println("This is child_Class .");
}
}
Now, let’s create object for the child class only which will
be enough to call the parent class and the grand parent class, ie, child
class calls the parent class and the parent class calls the grandparent class.
public static void main(String[] args) {
Child c = new Child();
c.methoGP();
System.out.println("The text in parent class is:"+c.text);
c.methodP();
c.methodC();
}
Now, when we run the main method, we get below output:
This is
Grand_Parent_Class .
The text
in parent class is:Hello
This is
Parent_Class .
This is child_Class .
Multiple
Inheritance:
In this there is one child and two parents . The syntax will
be like this:
public ChildClass extens Parent1Class , Parent2Class {
}
But, java does not support multiple inheritance. Java
compiler gets confused when it has to
call a function which is common to both parent1 class and parent2 class and you get compile
error.
Hybrid
It is combination of multiple and multilevel inheritances.
So, java does not support this inheritance as well.
Hierarchical
In this , there is one parent class and more than one child
classes. Each child classes are accessing the parent class property.
For example:
Create
a parent class Parent
public class Parent {
String text = "Hello";
public void methodP(){
System.out.println("This is Parent_Class .");
}
}
Let’s
create a child 1 class extended to the
parent class.
public class Child1 extends Parent {
public void methodC1(){
System.out.println("This is child1_Class .");
}
}
Let’s
create a child 2 class extended to the
parent class.
public class Child2 extends Parent {
public void methodC2(){
System.out.println("This is child2_Class .");
}
}
Now, let’s call Child1 class in the main
method:
public static void main(String[] args) {
Child1 c1 = new Child1();
c1.methodP();
System.out.println("The text in parent class is:"+c1.text);
c1.methodC1();
}
OUTPUT:
This is
Parent_Class .
The text
in parent class is:Hello
This is child1_Class
Now, let’s call Child2 class in the main method:
public static void main(String[] args) {
Child2 c2 = new Child2();
c2.methodP();
System.out.println("The text in parent class is:"+c2.text);
c2.methodC2();
}
OUTPUT:
This is
Parent_Class .
The text
in parent class is:Hello
This is child2_Class .
No comments:
Post a Comment