Single Inheritance in Java

Yukti Kumari
Last Updated: May 26, 2022

Introduction

Object-Oriented Programming, popularly known as OOPs, is one of the major pillars of java, known for its ease of usage. To get flawless control over your OOPs skills, one should have a strong grip over the four concepts - A PIE 🥧.

i.e., AbstractionPolymorphism, Inheritance, and Encapsulation

Inheritance allows one class to inherit the methods and variables from other classes, thus reusing the codes. In Java, we have different types of inheritance, namely, single inheritance, multiple, multilevel, and hybrid. Inheritance establishes an “is-a” relationship between two classes or a “parent-child” relationship.

Example - 

Suppose we have a class named “Human” and another class, “Employee”. Since an Employee IS A Human, the employee class can inherit the methods and fields of the human class.

In this article, we will go through the basic understanding of single inheritance in java along with examples and implementation.

Terms frequently used in Inheritance

  • Class: It is a user-defined template or blueprint from which objects are created.
  • Derived/Sub/Child class: It is a class that is derived from another class. Also known as extended class.
  • Base/Super/Parent class: It is a class from which the derived class inherits its features.
  • Reusability: It allows the reuse of the methods and fields of the existing class when creating a new class.

What is Single Inheritance?

Single inheritance is the simplest type of inheritance in java. In this, a class inherits the properties from a single class. The class which inherits is called the derived class or child class or subclass, while the class from which the derived class inherits is called the base class or superclass or parent class. So, in single inheritance, we have only one derived class and one base class.

Let us look at this diagram to understand the relation : 

 

Syntax of Single inheritance in Java:

 

class base class
{
     .... methods
}
class derivedClass name extends baseClass
{
    methods ... along with this additional feature
}

Java uses the keyword “extends” to create a new class(derived class) from the existing class(base class). The term “extends” means to increase the functionality as the derived class can reuse the methods and fields of the base class, and along with this, new methods and fields can also be defined in the derived class, hence increasing the functionality.

Code to illustrate Single inheritance in Java:

class Employee {
    void salary() {
        System.out.println("Salary= 200000");
    }
}

class Programmer extends Employee { 
// Programmer class inherits from Employee class
    void bonus() {
        System.out.println("Bonus=50000");
    }
}

class single_inheritance {
    public static void main(String args[]) {
        Programmer p = new Programmer();
        p.salary(); // calls method of super class
        p.bonus(); // calls method of sub class
    }
}

 

Output:

Salary= 200000
Bonus=50000

 

Explanation:

In the above example, we have two classes- The employee class and the Programmer class. 

We see that the Programmer class extends the Employee class, which is an example of single inheritance. The relationship “is a” is established here, i.e., a Programmer is an Employee. In the main method, we create an object ‘p’ of the class Programmer. p.salary() calls the method of the Employee class, which is inherited by the programmer class, and p.bonus() calls the method of the programmer class. 

Frequently Asked Questions

What is single-level inheritance? 

In single-level inheritance, the derived class inherits from a single base class only.

What are single and multiple inheritances in Java? 

In single inheritance, there is one derived class and one base class, whereas, in multiple inheritances, there can be two or more two base classes but a single derived class.

What is the difference between multiple and multilevel inheritances?

Multiple inheritances occur when a class inherits from many base classes, while multilevel inheritance is when a class inherits from a derived class and thus making this derived class a base class for the new class.

What are the different types of inheritance? 

The different types of inheritance are Single Inheritance, Multiple Inheritance, Multi-Level Inheritance, Hierarchical Inheritance, and Hybrid Inheritance.

Conclusion

In this article, we discussed Single Inheritance in Java, an important concept in Object-oriented programming, how it works, the syntax, and examples of implementing single inheritance in Java. 

There are 4 more types of inheritances in Object-Oriented Programming - Multiple inheritance, Multi-level inheritance, Hierarchical inheritance, and Hybrid inheritance.

You can learn more about Object-Oriented Programming and related concepts here.

You can also learn programming in java from scratch from our guided path -” Basics of Java,” which explains everything needed to get started with programming in java, like functions, loops, arrays, strings, and other Data Structures and Algorithms. 

Don’t forget to test your problem-solving skills by practicing problems from Codestudio, and also check out some Java Interview Questions as well as the numerous interview experiences specially curated to give you the right guidance.

Happy Coding!

Was this article helpful ?
3 upvotes

Comments

No comments yet

Be the first to share what you think