Inheritance is a mechanism that allows the class to use the states and behavior of another class. In simple words a class derive field and methods from another class. The derived class in inheritance is called sub class (also called derived class or extended class, or child class) and the class from which it is derived is called super class (also called base class or a parent class).
Important Note: A super class can have any number of sub class in inheritance but sub class can only extend one super class. Multiple inheritance is not supported in JAVA.
Inheritance is one of the important concept of object oriented programming. The simple definition says inheritance provides mechanism that allows a class to inherit properties of another class.
Inheritance Concept: Inheritance means inherit the properties and behavior of existing object in a new object.
Way To Achieve Inheritance: It is achieved by deriving a new class from existing class using extends keyword. Example, Class Child extends Base { }
.
Table of Contents
For example :
Suppose a class name Base.java
class Base { //Code of Base class }
Another class Child.java use Inheritance to extends properties from Base class.
Class Child extends Base { //extends the properties of base class }
In the above example, Child class will inherit field and methods of Base class.
There are 3 types of inheritance:
1. Single Inheritance:
In Single inheritance one class is derived from one parent class. The below diagram shows single inheritance where Class B inherits from Class A.
In multilevel inheritance there is a concept of grand parent class as shown in below diagram class C inherits class B and class B inherits class A.
In Hierarchical inheritance more than one sub classes is derived from a single parent class as shown in below diagram class B and C both are derived from single parent class A.
Why multiple inheritance is not supported in java:
To remove ambiguity:
Multiple inheritance is not supported in java as it causes ambiguity in few scenarios. The most common scenario is Diamond problem.
Below is the program to show you the use of inheritance in java. For coding this we have used eclipse IDE.
Example 1: Let’s inherit some fields and methods in Child class from Base class.
Base class is having 2 fields and 1 method:
class Base { int x=50; int y = 60; //Addition method return integer value of x+y public int addition(){ return x+y; //return 110 } }
Child class inherit addition method and x field from Base class:
public class Child extends Base { int z; public void subtraction(){ //addition method and x filed is inherited from Base Class z = addition() - x; System.out.println(z); } }
In the Main class we create Child object and calls subtraction method on it.
public class InheritanceAbhiandroid { public static void main(String[] args) { Child child = new Child(); //Child object child.subtraction();//Subtraction method called on child object } }
Output:
60
Example 2:
Base.java:
class Base { int x=50; }
Child.java:
public class Child extends Base { int x=20; void show() { System.out.println(x); } }
InheritanceAbhiandroid.java
public class InheritanceAbhiandroid { public static void main(String[] args) { Child c = new Child(); c.show(); } }
OUTPUT:
20
Important Note: The above program prints value of x=20 because priority always goes to local variables. So in show() method of child class,we can access member of parent class i.e base class using super keyword. It means if we want to print value of x=50 also from the same above program then by using super keyword.
Example 3 of Inheritance using super keyword:
Base.java:
class Base { int x=50; }
Child.java:
class Child extends Base { int x=20; void show() { System.out.println(x); System.out.println(super.x); } }
public class InheritanceAbhiAndroid{ public static void main(String[] args) { Child c=new Child(); c.show(); } }
OUTPUT:
20 50
Premium Project Source Code:
Leave a Reply