Single Inheritance in Java

Single Inheritance in Java

09 Sep 2024
Beginner
23.8K Views
10 min read
Learn with an interactive course and practical hands-on labs

Java Online Course Free with Certificate (2024)

What Is Inheritance in Java?

Inheritance is one of the powerful Object-Oriented Programming concepts in Java. In which a class can inherit attributes and behaviors from superclasses. This allows systematic designing and structuring of classes, enabling access to properties of different methods or classes. In this Java Tutorial, we are going to discuss specifically Single Inheritance in Java, which will include What is Single Inheritance in Java? and Why Use Single Inheritance? We will also explore Single inheritance in Java with example. So, Let's first discuss a little bit about "What is Single Inheritance ?".

Take your career to the next level by joining our comprehensive Java Full Stack Developer Course Training today!

What is Single Inheritance?

  • Single inheritance allows a child class to inherit properties and behavior from a single-parent class.
  • It enables code reusability as well as you can add new features to the existing code.
  • This makes the code less repetitive.
  • Single inheritance is more secure than multiple inheritance if it is approached in the right way.
  • It also enables a child class to call the parent class implementation for a specific method if this method is overridden in the child class or the parent class constructor.

Syntax of Single Inheritance in Java

class Childclass-name extends Parentclass-name  
{  
   //methods
}     

Example of Single Inheritance in Java

Let's Take an example and try it in our Java Compiler.
class Student{
    void Fee() {
        System.out.println("Student Fee= 20000");
    }
}
class Student_Name extends Student{ 
    void Name() {
        System.out.println("Student Name=Jayanti");
    }
}
class College {
    public static void main(String args[]) {
        Student_Name p = new Student_Name();
        p.Fee(); 
        p.Name(); 
    }
}

Output:

    Student Fee= 20000
    Student Name=Jayanti

In the above example, we have taken two classes- The Student class and the Student_Name class.

The Student_Name class extends the Student class, which is an example of a single inheritance. The relationship “is a” is established here, i.e., a Student_Name is a Student. In the main method, we create an object ‘p’ of the class Student_Name. p.Fee() calls the method of the Student class, which is inherited by the  Student_Name class, and p.Name() calls the method of the  Student_Name class. 

Why Do We Need Java Inheritance?

There are some important needs included in Java inheritance to achieve structured project code.

  • Encapsulation: In encapsulation when a code involves some common variables, The programmer encapsulates these in a parent class and simply provides particular attributes to child classes.
  • Polymorphism: In this, The same class acts differently to ensure according to the form of the Child’s class.
  • Code Reusability: Using Inheritance we can reuse the code whenever we want.

Types of inheritance in java

There are more 4 types of Inheritance in Java except Single inheritance
  • Multiple inheritances
  • Multi-level inheritance.
  • Hierarchical Inheritance.
  • Hybrid Inheritance.

1. Multiple Inheritance 

  • Java doesn’t support multiple inheritances in classes because it can create a diamond problem.
  • Consider Article Multiple Inheritance in Java for a better understanding of the diamond problem 

2. Multi-level inheritance.

  • In Multi-Level Inheritance, a class extends to another class that is already extended from another class.
  • For example, if A daughter extends the feature of the mother and the mother extends from the grandmother, then this scenario is known to follow Multi-level Inheritance.
  • As the name shows, numerous base classes are involved in multi-level inheritance.
  • The newly derived class from the parent class becomes the base class for another newly derived class.
  • The inherited features in multilevel inheritance in Java likewise come from several base classes.

Multi-level inheritance

3. Hierarchical Inheritance.

  • Hierarchical inheritance in Java is the sort of inheritance when many subclasses derive from a single class.
  • In short, It is a mixture of various inheritance types called hierarchical inheritance.

Hierarchical Inheritance

4. Hybrid Inheritance.

  • Hybrid Inheritance in Java is a combination of all inheritances.
  • Single and hierarchical inheritances or multiple inheritance.

Hybrid Inheritance

Master Java Full-Stack Development with ScholarHat’s hands-on certification programs and transform your coding career.

Training Name Price
Full-Stack Java Developer Certification Course (A Proven Curriculum to Get a Software Job of ₹3 - ₹9 LPA*Book a FREE Live Demo!

Benefits of Single Inheritance in Java

  • In single inheritance, a code can be used again and again
  • It enhances the properties of the class, which means that the property of the parent class will automatically be inherited by the child class
  • Inheritance allows access to properties and methods of a Parent class by a Child class. The "extend" keyword is used to inherit all the properties of the Parent classes by Child classes. (Subclass is a class derived from another class)

Limitations of Single Inheritance in Java

  • A Child class can only inherit from a single parent Class which results in less flexibility for class designs.
  • It can limit the ability to reuse code as some code might not fit in a hierarchy caused by the single inheritance.
  • It can cause ambiguity in code or throw errors, as some inherited methods can have the same names but have different operations.

Advantages and Disadvantages of Inheritance

AdvantagesDisadvantages
It allows for code reuse, saving you ample time and effort during software development.It creates a rigid and complex design that is hard to adapt or change, especially when the program requirements evolve with time.
It enables the creation of a class hierarchy, which is useful for modeling real-world objects and establishing their relationships. It can be hard to understand, particularly when an inheritance hierarchy or multiple inheritances are used. 
It divides code into parent and child classes, which helps to write more organized and modular code.It restricts code flexibility due to tight coupling.
It enables the creation of abstract classes defining a common interface for a group of related classes, which makes it easier to maintain and extend code. Decreased Code Performance due to sometimes of complexity.
Conclusion
Single Inheritance in Java is important for managing class flow. By creating a Base class that Child classes can inherit, developers can save time and make their code more organized and efficient. This makes it easier to function. We explored all concepts related to single inheritance with its real-time example. I would like to listen to your feedback on this. Also, consider our Java Full Stack Developer Course
 to become a master in Java.

FAQs

The concept of a subclass inheriting properties and behavior from a single superclass

  • Single-level inheritance.
  • Multi-level Inheritance.
  • Hierarchical Inheritance.
  • Multiple Inheritance.
  • Hybrid Inheritance.

 The Dog class inherits the eat() method from the Animal class.

Single inheritance, Multilevel inheritance.

To derive a class from another class
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this