Table of contents
1.
Introduction
1.1.
Terms frequently used in Inheritance
2.
What is Single Inheritance?
2.1.
Syntax of Single Inheritance in Java
2.2.
Code to illustrate Single Inheritance in Java
2.3.
Java
2.4.
Explanation
3.
Benefits of Single Inheritance in Java
4.
Limitations of Single Inheritance in Java
5.
Examples of Single Inheritance in Java Programming
5.1.
Java
5.2.
Java
6.
Using Super() and Subclass Constructors in Single Inheritance in Java
6.1.
Java
7.
Why Do We Need Java Inheritance?
7.1.
1. Encapsulation of Common Features
7.2.
2. Polymorphism for Flexible Behavior
7.3.
3. Reusability of Code
7.4.
4. Better Maintainability
7.5.
5. Improves Project Structure
8.
Frequently Asked Questions
8.1.
What is a real life example of single inheritance in Java?
8.2.
What is inheritance single & multilevel in Java?
8.3.
What are the 4 types of inheritance in Java?
9.
Conclusion
Last Updated: May 11, 2025
Easy

Single Inheritance in Java

Author Yukti Kumari
6 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Single Inheritance in Java simplifies class hierarchies by allowing a subclass to inherit properties and behaviors from a single superclass. It's achieved using the 'extends' keyword, promoting clean and maintainable code.

single inheritance program in java

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 : 

 

Single Inheritance

Syntax of Single Inheritance in Java

class base class
{
     .... methods
}
class derivedClass name extends baseClass
{
    methods ... along with this additional feature
}
You can also try this code with Online Java Compiler
Run Code

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

  • Java

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
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Salary= 200000
Bonus=50000


Try this code by yourself on Online Java Compiler.

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. 

Single inheritance in Java has several benefits and limitations. Here are some of them:

Also see, Characteristics of OOPS

Benefits of Single Inheritance in Java

  • Single inheritance makes the code easy to read and manage by following a clear structure. It also helps avoid repeating the same code.
     
  • It supports code reusability, as the child class can use the methods and properties of the parent class.
     
  • It allows better abstraction because the child class can have its own features along with those it inherits from the parent class.

Limitations of Single Inheritance in Java

  • A class can extend only one parent class, which reduces flexibility in designing programs.
     
  • It limits code reuse in cases where certain functions don’t fit well in a single hierarchy.
     
  • It may lead to confusion or errors if different classes have methods with the same name but perform different tasks.

Examples of Single Inheritance in Java Programming

Let us take two examples of single inheritance:

Example 1:

  • Java

Java

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.bark();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

output

Example 2:

  • Java

Java

class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}

class Circle extends Shape {
void drawCircle() {
System.out.println("Drawing a circle");
}
}

public class Main {
public static void main(String[] args) {
Circle myCircle = new Circle();
myCircle.draw(); // Inherited from Shape
myCircle.drawCircle();
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

output

Using Super() and Subclass Constructors in Single Inheritance in Java

In Java, when implementing single inheritance, the super() keyword is used in a subclass constructor to invoke the constructor of its superclass. This is crucial for initializing inherited attributes and ensuring proper construction of the object hierarchy.

Consider the following example:

  • Java

Java

class Animal {
String sound;

Animal(String sound) {
this.sound = sound;
}

void makeSound() {
System.out.println("Animal makes a sound: " + sound);
}
}

class Dog extends Animal {
String breed;

Dog(String sound, String breed) {
super(sound); // Invoking superclass constructor
this.breed = breed;
}

void displayInfo() {
System.out.println("Dog breed: " + breed);
}
}

class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Woof", "Labrador");
myDog.makeSound(); // Inherited from Animal
myDog.displayInfo();
}
}
You can also try this code with Online Java Compiler
Run Code

Output

output

In this example, the Dog class extends the Animal class. The super(sound) line in the Dog constructor invokes the constructor of the Animal class, initializing the sound attribute. This demonstrates the use of super() and subclass constructors in the context of single inheritance in Java.

Why Do We Need Java Inheritance?

Inheritance in Java makes the code well-structured, readable, and easy to manage. It helps developers avoid repeating the same code and supports better software design by allowing classes to build upon existing ones.

Below are the key reasons why Java inheritance is important:

1. Encapsulation of Common Features

In many programs, different classes may share some common variables or methods. Instead of writing the same code in every class, we can keep these shared elements in a parent class. The child classes can then use or update them as needed. This helps in organizing the code properly and makes updates easier in the future.
 

2. Polymorphism for Flexible Behavior

Inheritance allows polymorphism, which means methods in the parent class can behave differently when used by child classes. This flexibility makes the program more dynamic and helps us write cleaner and smarter code. We can define a method once in the parent class and then change its behavior in the child class if needed.
 

3. Reusability of Code

One of the biggest advantages of inheritance is code reusability. Instead of writing similar code again and again, we can define it once in a base class and use it wherever required. This not only saves development time but also reduces the chance of bugs.
 

4. Better Maintainability

Since common logic is written only once, updating or fixing it becomes much easier. If you make changes in the parent class, all child classes that inherit from it will automatically get those updates. This makes the maintenance of large projects faster and simpler.
 

5. Improves Project Structure

Using inheritance gives your project a logical structure. It allows you to create a hierarchy of classes, where higher-level classes provide general behavior and lower-level classes handle more specific tasks. This makes the application easier to scale and understand for any developer working on the project.
 

Frequently Asked Questions

What is a real life example of single inheritance in Java?

An example is a "Car" class inheriting from a "Vehicle" class. The "Car" class gains properties like speed and methods like start() from the "Vehicle" class.

What is inheritance single & multilevel in Java?

Single inheritance involves a class inheriting from one superclass, while multilevel inheritance includes a chain of classes, each extending the one above it. In Java, this forms a hierarchy facilitating code organization and reuse.

What are the 4 types of inheritance in Java?

The four types of inheritance in Java are single, multilevel, hierarchical, and hybrid. Single, multilevel, and hierarchical are straightforward, while hybrid involves combining these types, typically using interfaces.

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. 

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

 

Live masterclass