• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Inheritance in Java

Inheritance in Java

Last Updated on: April 22, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Introduction Of Inheritance in Java
  • Important Terminology Of Inheritance
  • Inheritance Syntax in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multi-Level Inheritance in Java
  • Hierarchical Inheritance in Java
  • Multiple Inheritance in Java
  • Hybrid Inheritance in Java

Inheritance in Java Programming: In Our Previous Post, we discussed different Java access modifiers, and in this post, we are going to discuss one of the main OOPS concepts of Java called Inheritance.

To be a Good Java developer, you must have good knowledge of Java OOPS concepts like Inheritance, Abstraction, Encapsulation, and Polymorphism. So in this post, we will discuss one of the important concepts of the OOPS concept, which is Inheritance, and we will also learn how we can achieve inheritance in the Java programming language.

In this post. We are going to discuss the following:

  • Introduction of Inheritance
  • Types Of Inheritance
  • Important Fact about Inheritance

Introduction Of Inheritance in Java

The process by which one class acquires the data member (Variables) and member functions (methods) of another class that process is called inheritance. The main benefits of Inheritance are it provides code reusability so that a class can extend the properties and behaviors of an existing class. After that, they need to write only the unique new features.

Important Terminology Of Inheritance

  • Parent Class: A Class whose properties and member functions are inherited by another class is known as a parent class, superclass, or base class.
  • Child Class: A Class that extends the properties and behaviors of another class is called a child class, subclass, or derived class.

Inheritance Syntax in Java

For Inherit the data member and member functions, we are using the keyword “Extends”. Suppose there are two classes (Class1 & Class2) present, where class2 inheriting the properties and methods of class1, Then we have to write like below

Class class2 extends class1
{
//Body
}

Types of Inheritance in Java

In Java, there are mainly five types of inheritance are there, that is:

Single Inheritance
Hierarchical inheritance
Multiple Inheritance
Multi-Level Inheritance
Hybrid Inheritance

Single Inheritance in Java

In this type of inheritance, there are two classes present one is the parent class, and another one is the child class. Here child class inherits the properties of the parent class into the child class. By this, we can reuse the existing code and add new features in the child class.

Single Inheritance

Single Inheritance Example:

package com.SoftwareTestingo.JavaBasics;
class Single_ParentClass
{
   public void parent_method()
   {
      System.out.println("Parent Class Method");
   }
}
public class Single_Inheritance extends Single_ParentClass
{
   public void child_method()
   {
      System.out.println("Child class Method");
   }
   public static void main(String[] args) 
   {
      Single_Inheritance obj=new Single_Inheritance();
      obj.parent_method(); //Calling Parent Class Method
      obj.child_method();  //Calling Child Class Method
   }
}

Output:

Parent Class Method
Child class Method

Multi-Level Inheritance in Java

When a Class has more than one parent class, that type of inheritance is called multilevel inheritance. If you saw the pic, you can find that Class B inherits members and methods of Class A, and Class C extends class b.

Multi Level Inheritance

Multi-Level Inheritance:

package com.SoftwareTestingo.JavaBasics;
class MultiLevel_ParentClass
{
   public void parent_method()
   {
      System.out.println("Parent Class Method");
   }
}
class Middle_Class extends MultiLevel_ParentClass
{
   public void middleClass_method()
   {
      System.out.println("Middle Class Method");
   }
}
public class MultiLevel_Inheritance extends Middle_Class
{
   public void child_method()
   {
      System.out.println("Child Class Method");
   }
   public static void main(String[] args) 
   {
      MultiLevel_Inheritance obj=new MultiLevel_Inheritance();
      obj.parent_method(); //Calling Grand Parent Method
      obj.middleClass_method(); // Calling Parent Method
      obj.child_method();  //Calling Child Method	
   }
}

Output:

Parent Class Method
Middle Class Method
Child Class Method

Hierarchical Inheritance in Java

In this type of inheritance, one class is served as a base class, and multiple subclasses extend the features and methods of that base class. As you saw in the diagram, Class A Is the base class, and Class B, Class C, And Class D are the subclasses extending Class A.

Hierarchical Inheritance

Hierarchical Inheritance Example:

package com.SoftwareTestingo.JavaBasics;
class parent_class
{
   public void parent_method()
   {
      System.out.println("Parent Class Method");
   }
}
class ClassA extends parent_class
{
   public void classAMethod()
   {
      System.out.println("Class A Method");
   }
}
class ClassB extends parent_class
{
   public void classBMethod()
   {
      System.err.println("Class B Method");
   }
}
class ClassC extends parent_class
{
   public void ClassCMethod()
   {
      System.out.println("Class C Method");
   }
}
public class Hierarchical_Inheritance 
{
   public static void main(String[] args)
   {
      ClassA obj1=new ClassA();
      ClassB obj2=new ClassB();
      ClassC obj3=new ClassC();
      
      // Call All Classes Method by Using above objects
      
      obj1.classAMethod();
      obj2.classBMethod();
      obj3.ClassCMethod();
   }
}

Output:

Class A Method
Class B Method
Class C Method

Multiple Inheritance in Java

In Multiple inheritances, one class has more than one base class and inherits features from all its parent classes. But Java doesn’t support multiple inheritances with classes, But we can achieve multiple inheritances by implementing Interface.

Multiple Inheritance

Multiple Inheritance Example:

package com.SoftwareTestingo.JavaBasics;
class ParentClass1
{
   public void parentMethod1()
   {
      System.out.println("Parent Method Called");
   }
}
class ParentClass2
{
   public void parentMethod2()
   {
      System.out.println("Parent Method Called");
   }
}
//Java Does Not Support Multiple Inheritance
//public class Multiple_Inheritance extends ParentClass1, ParentClass2
//{
//	public static void main(String[] args) 
//	{
//		Multiple_Inheritance obj=new Multiple_Inheritance();
//		obj.parentMethod1();
//		obj.parentMethod2();
//	}
//}

You Will get below errors:

Multiple Inheritance In Java Error

Hybrid Inheritance in Java

The combination of one or more types of inheritance is called Hybrid inheritance.

Points to Remember

  • Multiple Inheritance is Not allowed in Java: Multiple inheritances mean a class has more than one parent class. Because of these features, there is a chance of ambiguous situations. That’s why multiple inheritances are not permitted in Java.
Hybrid Inheritance

Note: Multiple inheritances are not permitted in Java, but you can achieve that by using Interface.

  • Cyclic Inheritance Is not allowed: It is a type of inheritance where a class extends itself or by some loop. Such inheritance is not allowed in Java because this leads to ambiguity.
Cyclic Inheritance
  • Private Members Are Not Inherited
  • Constructors Can Not be Inherited In Java

Constructors can not be inherited in Java as the subclass have a different name. But when we are creating the object of the subclass, that time default constructor of the parent class is called by default. It is all happening because when the child class constructor is called, the first statement of a child class constructor is a super keyword. That’s why when we create a child class object, the parent class-default constructor is also called automatically.

Because of the super keyword, we can call the parent class constructor. Other than this, we can not be permitted to call the parent class constructor directly.

 DefaultPrivateProtectedPublic
Same ClassYesYesYesYes
Same Package SubclassYesNoYesYes
Same Package Non-SubclassYesNoYesYes
Different Package SubclassNoNoYesYes
Different Package Non-SubclassNoNoNoYes

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers