Home >>Java Tutorial >Inheritance in Java
A mechanism that involves the acquiring of all the properties and behaviors of the parent object by the child object is known as Inheritance in Java.
This is considered as the important part of the OOPs (Object Oriented programming system) and the concept behind inheritance in Java language is simple as the programmer can create new classes that are built on the base of existing classes. Whenever the programmer inherits from an existing class then it simply means that the programmer can reuse the methods and fields of the parent class. Apart from that, the programmer can also add some new methods and the fields in their current class.
The relationship that is represented by the inheritance in Java is usually the IS-A relationship that is also seen as the parent-child relationship.
There are mainly two uses of the inheritance in Java that are depicted below:
It is usually possible to inherit the attributes and the methods in Java language from the one class to another. Hence, the concept of inheritance can be divided in to two parts that are depicted below:
Use of the extends keywords is done in order to inherit from a class.
Here is the syntax of the Java inheritance depicted below for your understanding:
class Subclass-name extends Superclass-name { //methods and fields }
In the above mentioned syntax there is a mention of the extends keyword, this keyword basically indicates that the programmer is creating a new class that is being derived from an existing class. Here in this context the meaning of the "extends" is basically to increase the functionality. And in simple terminology of Java language, the class from which the data is inherited is known as the parent class or superclass and the class that inherits the data is known as the child class or the subclass.
Here is an example of the Java inheritance that will make you understand the physical aspect of it. Here is this example the relation of the parent class and the child class is depicted and the process of inheritance is explained in the most simplified way:
class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
There are various terminologies that are associated with the Java inheritance that are usually used frequently. Here are some of the terminologies that are depicted below:
The types of inheritance are dependent of the Java class and according to that there are basically three types of inheritance that are present in the java language that are depicted below:
1. Single inheritance is basically the inheritance where it takes place between only on class.
Here is an example that will depict the concept of single class inheritance in Java programming language:
class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class Cat extends Animal{ void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }}
2. Multilevel inheritance in the Java language is basically the phenomenon when a single class inherits the attributes and the methods of the multiple classes of various level.
Here is an example that will depict the concept of multilevel inheritance in Java programming language:
class Animal { void eat () { System.out.println ("eating..."); } } class Dog extends Animal { void bark () { System.out.println ("barking..."); } } class BabyDog extends Dog { void weep () { System.out.println ("weeping..."); } } class TestInheritance2 { public static void main (String args[]) { BabyDog d = new BabyDog (); d.weep (); d.bark (); d.eat (); }}
3. Hierarchical Inheritance in Java language is basically the situation where one class serves as a superclass (base class) for more than one sub class.
Here is an example that will depict the concept of hierarchical inheritance in Java programming language:
// Java program to illustrate the // concept of Hierarchical inheritance import java.util.*; import java.lang.*; import java.io.*; class one { public void print_php() { System.out.println("PHPTPOINT"); } } class two extends one { public void print_me() { System.out.println("Jerry"); } } class three extends one { /*............*/ } // Drived class public class Main { public static void main(String[] args) { three g = new three(); g.print_phpa(); two t = new two(); t.print_me(); g.print_php(); } }
Please note that there are two more types of the Java inheritance but that can only be used through the help of interfaces are depicted below:
4. Multiple Inheritance in the Java language is basically the situation in which one class can have more than one superclass and it can inherit features from all the parent classes.
Here is an example that will depict the concept of multiple inheritance in Java programming language:
// Java program to illustrate the // concept of Multiple inheritance import java.util.*; import java.lang.*; import java.io.*; interface one { public void print_php(); } interface two { public void print_me(); } interface three extends one,two { public void print_php(); } class child implements three { @Override public void print_php() { System.out.println("PHPTPOINT"); } public void print_me() { System.out.println("Jerry"); } } // Drived class public class Main { public static void main(String[] args) { child c = new child(); c.print_php(); c.print_me(); c.print_php(); } }
5. Hybrid inheritance in Java language is basically a mix of two or more of the above mentioned types of inheritance.
The basic reason for the multiple inheritance to not get supported by the Java language is that without the use of multiple inheritance the complexity is reduced and language remains simplified. For instance, suppose a situation where A,B and C are the three classes in Java. Now, the A and B classes are inherited by the class C. Now, in case there are same methods that are present in the class A and B and if the programmer calls it from the class object then there will arise a situation that will cause ambiguity to call the method of B or A class.
As a matter of fact, we know that compile-time errors are usually better than the runtime errors hence; Java renders the compile-time error if the programmer inherits 2 classes. Therefore, whether the programmer has same method or different, there will always be a compile time error.
Here is an example of the situation that will help you understand the physical aspect of it along with the concept:
class A { void msg () { System.out.println ("Hello"); } } class B { void msg () { System.out.println ("Welcome"); } } class C extends A, B { //suppose if it were public static void main (String args[]) { C obj = new C (); obj.msg (); //Now which msg() method would be invoked? } }