Home >>Java Tutorial >Inheritance in Java

Inheritance in Java

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.

The uses of the inheritance in Java language

There are mainly two uses of the inheritance in Java that are depicted below:

  • For Method Overriding in order to achieve the polymorphism.
  • For the Code Reusability.

Parts of inheritance in Java

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:

  • subclass (child) – This is basically the class that inherits from the another class
  • superclass (parent) – This is the class from which the methods and attributes get inherited from.

Use of the extends keywords is done in order to inherit from a class.

The syntax of Java Inheritance

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.

Java Inheritance Example

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);  
}  
}
Output:
Programmer salary is:40000.0
Bonus of Programmer is:10000

Terminology of the Inheritance in Java language

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:

  • Class: A class is basically a group of objects that are known to have similar or common properties. For the purpose of the object creation, these classes are used as a template or blueprint.
  • Sub Class/Child Class: Subclass is basically a class that is also known as derived class, child class, or extended class and it does the work of inheriting the other classes.
  • Super Class/Parent Class: Superclass is basically a class in Java that are used as a source for the subclass to inherits the method and attributes or the features. This class in Java language is also known as a base class or a parent class.
  • Reusability: The meaning of reusability is obvious from its name, in simple terms, a mechanism that facilitates the programmer to reuse the fields and methods of an existing class whenever the programmer is trying to create a new class is known as reusability. In terms of application point of view, the programmer or the user can use the exact same fields and the methods that are already defined in the previous class.

Types of inheritance in java

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:

  • Single
  • Multilevel
  • Hierarchical

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  
}}  
Output:
meowing...
eating...

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 ();

}} 
Output:
weeping...
barking...
eating...

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(); 
	} 
}
Output:
PHPTPOINT
Jerry
PHPTPOINT

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(); 
	} 
}
Output:
PHPTPOINT
Jerry
PHPTPOINT

5. Hybrid inheritance in Java language is basically a mix of two or more of the above mentioned types of inheritance.

The reason for which the multiple inheritance is not supported in java

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?  
  }
}

No Sidebar ads