AppDividend
Latest Code Tutorials

Java Inheritance Tutorial With Example | Inheritance in Java

0

Java Inheritance Tutorial With Example | Inheritance in Java is today’s topic. Inheritance is a property of Object-Oriented Programming Concepts by which we can access some methods of a class in another class. The class whose methods is inherited know as Parent class/ Base class/ Superclass, and the class which is derived from Parent class is known as Child class/Subclass.

Now we’ll look into how to derive a base class from the superclass. We use extends the keyword to inherits a class.

Java Inheritance Tutorial With Example

The concept behind inheritance in Java is that you can create the new classes that are built upon existing classes. When you inherit methods and properties from an existing class, you can reuse the methods and fields of a parent class. Also, you can add new methods and fields in your current class also.

Syntax

The syntax of Java Inheritance is following.

class base_class_name extends super_class_name
{  
   // code here like methods and fields
}

Some important points about inheritance:

  1. Any sub can inherit all the members (all fields, member functions, and nested classes) from its superclass.
  2. Constructors are not considered as member functions; that’s why a subclass can not inherit it, but we can invoke a constructor of the superclass.
  3. A subclass inherits one superclass one at a time, but a class can have multiple subclasses.

Why use Inheritance in Java

Java is an Object Oriented Programming Language. Inheritance is one of the most popular concepts in Object Oriented Programming.

We use Inheritance in Java for the following reasons:

  1. Encapsulation: If we have the common attributes, then we encapsulate these in the parent class and give specific attributes to the child classes.
  2. Polymorphism: We can also use the Polymorphism with an Inheritance in Java. So that based on a type of child class, the same class behaves differently.  Runtime polymorphism cannot be achieved without using the inheritance.
  3. Code Reusability: Using the Inheritance, we can create reusable code.
  4. Efficiency: It is efficient to use Inheritance while writing code. This can increase the speed of the project.
  5. We can simulate an inheritance of classes with the real-time objects, which makes the OOPs more realistic.
  6. Inheritance provides the data hiding. The base class can hide some data from a derived class by making it private.

Types of Inheritance in Java

There are five types of inheritance.

  1. Single or Simple Inheritance
  2. Multiple Inheritance (Through Interface)
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance (Through Interface)

 

Java Inheritance Tutorial With Example

Now we are going to discuss those four different types of inheritance, one by one as Java doesn’t support multiple inheritances. We’ll discuss later on in this topic.

Single or Simple Inheritance

In this inheritance, the subclass is derived from its superclass. Generally, in single inheritance, there are only one superclass and one subclass. It’s also known as Single level inheritance.

 

In the above diagram, Class A is extending by Class B. Here, Class A is parent/superclass of Class B, which is child/subclass.

Let’s understand Simple Inheritance with the help of an example.

// Simple.java

class Person
{
	int age,id;
	String name;
	void naming(String name)
	{
		System.out.println("Name:"+name);
	}
}
class Student extends Person
{
	void ageN(int age)
	{
		System.out.println("Age of student is:"+age);
	}
}
class Simple
{
	public static void main(String [] er)
	{
		Student s= new Student();
		s.naming("Robert");
		s.ageN(14);
	}
	
}

See the below output.

Single Inheritance

 

Multilevel Inheritance

When a subclass is derived from a derived class, then this type of inheritance is known as Multilevel inheritance. In the Multilevel Inheritance, the derived class will be inheriting the parent class and as well as the derived class act as a parent class to other class.

Multilevel Inheritance

 

As per the above diagram Class A extending by Class B and Class B is extending by class C.

Now we will discuss it a with the help of an example.

// Multilevel.java

//creating a class

class Student
{
	
	void printIden()
	{
		System.out.print("Student's ");
	}
}
//extending Student class
class BTech extends Student
{
	void Degree()
	{
		System.out.println("Degree is Bachelor of Technology");
	}
}
//extending BTech class
class CSE extends BTech
{
	void Stream()
	{
		System.out.println("Stream is Computer Sciencec and Engineering");
	}
}
//extending CSE class
class CyberSecurity extends CSE
{
	void Domain()
	{
		System.out.println("Domain is Cyber Security");
	}
}
//driver class 
class Multilevel
{
	public static void main(String []er)
	{
		BTech bt=new BTech();
		bt.printIden();
		bt.Degree();
		CSE cse =new CSE();
		cse.printIden();
		cse.Stream();
		CyberSecurity cs =new CyberSecurity();
		cs.printIden();
		cs.Domain();
	}
}

See the below output.

Multilevel Inheritance Example

 

In this example, we have seen that Student class is inherited by BTech class further BTech class is extended by CSE class, and CyberSecurity class inherits CSE class.

Multiple Inheritance

When one class inherits more than one classes is known as Multiple inheritance. But as we mentioned before that java does not support multiple inheritance because of ambiguity.

Suppose, Class C inherits Class A and Class B. If there is the same method in both of these classes these, it will create an ambiguity whenever we call the standard method using child class object. Still, we try to create these types of situation then it will show us compile-time error rather than run time error (because a compile-time error is better than run time error).

 

Multiple Inheritance

In the above diagram, Class A and Class B are extending by Class C.

We can achieve the Multiple Inheritance in Java through Interfaces.

See the below program.

// Multiple.java

// declaring interface Pet
interface Pet
{
	public void Show();
	
}
//declaring interface Animal
interface Animal
{
	public void Type();
}
//Multiple inheritance using interface 
class Dog implements Pet, Animal
{
	//decalring methods 
	public void Show()
	{
		System.out.println("It is a Pet");
	}
	//declaring methods 
	public void Type ()
	{
		System.out.println("It is an Animal");
	}
}
//driver class 
class Multiple
{
	public static void main(String []er)
	{
		Dog d=new Dog();
		d.Show();
		d.Type();
	}
}

The output is below.

Multiple Inheritance Example

 

Why multiple inheritance is not supported in java

Consider the scenario where A, B, and C are three classes. The class C inherits class A and B. If A and B classes have the same method and you call it from the child class object, they will create an ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit two classes. So whether you have a same method or different, there will be compile time error.

Hierarchical Inheritance

In this Inheritance, one Class is extending by many other subclasses. In Hierarchical inheritance, one parent class will be inherited by many subclasses.

Hierarchical Inheritance

 

In the above diagram, Class B, Class C, and Class D inherit the superclass A. See the following code.

// Hierarchical.java

//creating a class 
class Person
{
   int id;
   //creating a common method 
   void printId(int id)
	{
		System.out.println("ID:"+id);
	}
   
}
//extending person class
class Student extends Person
{
	
	void iden()
	{
		System.out.print("Student's");
	}
	
}
//extending person class
class Teacher extends Person
{
	void iden()
	{
		System.out.print("Teacher's ");
	}
}
//extending person class 
class Clark extends Person
{
	void iden()
	{
		System.out.print("Clark's");
	}
}
//driver class 

class Hierarchical
{
	public static void main(String [] er)
	{
		Student s=new Student();
		s.iden();
		s.printId(11701200);
		Clark c=new Clark();
		c.iden();
		c.printId(1203);
		Teacher t= new Teacher();
		t.iden();
		t.printId(11020);
		
	}
}

See the Output.

Hierarchical Inheritance Example

 

In this above example, we have a class name Person who has a standard method printId() which is being used by Student, Teacher, and Clark class.

Hybrid Inheritance

If there is class A and it is extending by class B and class C. Further, if class D inherits both class B and class C, then this type of inheritance is known as Hybrid Inheritance. It will also cause an error as class D inherits multiple classes, i.e. class B and class C.

Hybrid Inheritance       

 Hybrid Inheritance is a combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through the interface we can achieve this.

Finally, Java Inheritance Tutorial With Example | Inheritance in Java article is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.