Back

Single Inheritance in Java: Definition, Examples & Benefits

04 Feb 2025
4 min read

Inheritance in Java is a way in which one object gets all the properties and behaviours of a parent object. This and single inheritance in Java are key concepts in OOP (Object-Oriented Programming). 

With inheritance, we can build new classes on top of existing ones. It helps reuse the methods and fields from the parent class while letting you add new methods and fields in the current class. This makes coding cleaner and reduces repetition. Single inheritance in Java is the simplest form of this concept, where one class directly inherits from another.

What is Inheritance?

Inheritance in Java lets a class inherit properties and actions from another class, called the parent class or superclass. The class that gets these features is called the child class or subclass. This means the subclass can use the fields and methods of the parent class, follow reuse rules, and create a clear hierarchy.

Inheritance shows an "IS-A" relationship, also known as a parent-child relationship. It’s a fundamental concept that makes coding more straightforwardt.

Inheritance is essential for several reasons. First, it promotes code reusability by allowing child classes to use the standard code defined in the parent class without rewriting it. Second, it enables method overriding, which is necessary for runtime polymorphism in Java. Finally, it supports abstraction and lets you focus on essential functionalities while hiding unnecessary details. 

is-a relationship

In Java, inheritance follows an is-a relationship, meaning one class is a specific type of another class. This relationship is used when a child class logically belongs to a parent class. For example:

Car is-a Vehicle → A car has all the properties of a vehicle.

Apple is-a Fruit → An Apple is a type of fruit.

Surgeon is-a Doctor → A surgeon is a specialized doctor.

Dog is-a Animal → A dog is a kind of animal.

In these the child class can inherit properties and behaviors from the parent class. Hence Apple can inherit from Fruit and Dog can inherit from Animal. 

Frequently Used Terms in Inheritance

1. Class: A class is like a blueprint or template used to create objects. It defines the common characteristics (like behaviour) and properties (like attributes) all its objects share. A class isn’t a real-world entity; it’s just the structure for making objects.

2. Super Class/Parent Class: The superclass, also called the parent class, is the class whose features are passed down to another class. Think of it as the base class that provides fields and methods to its child classes.

3. Subclass/Child Class: The subclass, or child class, is the one that inherits the features of the parent class. In addition to using the fields and methods of the parent class, it can also add its unique fields and methods, making it more specific.

4. Reusability: Inheritance promotes reusability. You don't have to start from scratch if you already have a class with some of the code you need. Instead, you can create a new class that extends the existing one. This way, you reuse the parent class’s fields and methods, saving time and effort.

Why Do We Need Inheritance in Java?

Simply put, inheritance allows code reuse and avoids writing the same logic again. It also makes programs easier to manage by organising classes in a hierarchy. With inheritance, a child class can use the methods and properties of a parent class so duplication can be reduced.

For example, if we have a Vehicle class with a move() method, we don’t need to write move() again in Car and Bike classes. They can just inherit it. This makes coding simple, saves time, and helps in software development, where multiple classes share standard features.

Types of Inheritance in Java

There are five types of inheritance: 

1. Single Inheritance 

In this type, a subclass inherits directly from another superclass. It’s the simplest form of inheritance where a child class gets all the features of a parent class. It is also called simple inheritance. 

2. Multilevel Inheritance 

This involves a chain of inheritance. A class inherits from a class, and then another class inherits from it. It’s like a grandparent, parent, and child relationship.

3. Hierarchical Inheritance

In this case, multiple child classes inherit from a single parent class. All child classes share the properties and methods of the same parent. It’s like multiple classes sharing traits from the same parent.

4. Multiple Inheritance 

A class can inherit features from more than one parent class. However, in Java, multiple inheritance is supported only through interfaces to avoid conflicts.

5. Hybrid Inheritance 

This combines two or more types of inheritance, like hierarchical and multiple. In Java, hybrid inheritance is achieved using interfaces for flexibility and conflict management.

What is Single Inheritance in Java?

Single inheritance means that a child class inherits from just one parent class. It’s the most basic type of inheritance in Java. Therefore, it’s also called simple inheritance. In this setup, the child class gets all the properties and behaviours of its parent class.

For example, if there’s a parent class named A and a child class named B, the child class B will inherit everything from A. This includes methods and fields, so you don’t have to write the same code again. At the same time, the child class can add its own fields and methods. Therefore, it can also make it more specific while still using the foundation provided by the parent class.

custom img

How Does Single-Level Inheritance Work in Java?

custom img

Syntax of Single Inheritance in Java

The syntax for single inheritance in Java is straightforward. You use the extends keyword to make a child class inherit from a parent class. Here’s how it looks:

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

In this structure, the child class automatically inherits all accessible methods and fields from the parent class. Hence, it is easy to reuse and extend functionality.

Program for Single Inheritance in Java

Now let’s look at a few exmples for single inheritance in Java:

Algorithm

1. Start

2. Define a class Vehicle:

  • Create a method start() that prints "Vehicle is starting..."
  • Create a method stop() that prints "Vehicle is stopping..."

3. Define a class Car that inherits from Vehicle

  • Create a method honk() that prints "Car is honking..."

4. Define the Main class. Inside the main() method:

  • Create an object myCar of class Car
  • Call start() method using myCar (inherited from Vehicle)
  • Call honk() method using myCar
  • Call stop() method using myCar (inherited from Vehicle)

5.End

Code

// Parent class
class Vehicle {
    void start() {
        System.out.println("Vehicle is starting...");
    }

    void stop() {
        System.out.println("Vehicle is stopping...");
    }
}

// Child class
class Car extends Vehicle {
    void honk() {
        System.out.println("Car is honking...");
    }
}

// Main class
public class Main {  // The name matches the file name
    public static void main(String[] args) {
        Car myCar = new Car();  // Create an object of the Car class
        myCar.start();          // Call method from the Vehicle class
        myCar.honk();           // Call method from the Car class
        myCar.stop();           // Call method from the Vehicle class
    }
}

How The Code Works

The code shows how a child class can inherit and extend the functionality of a parent class:

  1. Parent Class (Vehicle): Vehicle defines two methods: start() and stop(), representing basic vehicle actions.
  2. Child Class (Car): Car extends Vehicle, inheriting its methods and adding its own method, honk(), for car-specific functionality.
  3. Main Class (Main): In the main() method, an object of Car is created. The object can use the inherited start() and stop() methods, as well as its own honk() method.

Output

Vehicle is starting...
Car is honking...
Vehicle is stopping...

=== Code Execution Successful ===

Second Example: Single Inheritance in Java 

Let’s look at another example of the program for single inheritance in Java. Here an Animal class is the parent class, and a Bird class inherits from it. The Bird class adds the specific behaviour of making a sound, which is a "chirp":

Algorithm

1. Start

2. Define a class Animal

  • Create a method makeSound() that prints "Animal makes a sound..."

3. Define a class Bird that inherits from Animal

  • Override the makeSound() method to print "Bird chirps..."

4. Define the Main class. Inside the main() method:

  • Create an object myBird of class Bird
  • Call makeSound() method using myBird (executes overridden method from Bird class)

5. End

Code

// Parent class
class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound...");
    }
}

// Child class
class Bird extends Animal {
    void makeSound() {
        System.out.println("Bird chirps...");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Bird myBird = new Bird();  // Create an object of the Bird class
        myBird.makeSound();        // Call the overridden method from Bird class
    }
}

How The Code Works

The code demonstrates how a subclass can inherit and override a method to provide specific functionality.

  • The Animal class has a generic makeSound() method.
  • The Bird class, which extends Animal, overrides the makeSound() method to specify that a bird "chirps".
  • When you create an object of the Bird class and call makeSound(), it prints "Bird chirps...".

Output

Bird chirps...

=== Code Execution Successful ===

Third Example: Single Inheritance in Java 

Here we have an Appliance class (the parent class) and a WashingMachine class (the child class). The washing machine is a type of appliance with a specific behaviour: washing clothes.

Algorithm 

1. Start

2. Define a class Appliance

  • Create a method turnOn() that prints "Appliance is now ON."
  • Create a method turnOff() that prints "Appliance is now OFF."

3. Define a class WashingMachine that inherits from Appliance

  • Create a method washClothes() that prints "Washing machine is washing clothes..."

4. Define the Main class. Inside the main() method:

  • Create an object myWasher of class WashingMachine
  • Call turnOn() method using myWasher (executes inherited method from Appliance class)
  • Call washClothes() method using myWasher (executes method from WashingMachine class)
  • Call turnOff() method using myWasher (executes inherited method from Appliance class)

5. End

Code

// Parent class
class Appliance {
    void turnOn() {
        System.out.println("Appliance is now ON.");
    }

    void turnOff() {
        System.out.println("Appliance is now OFF.");
    }
}

// Child class
class WashingMachine extends Appliance {
    void washClothes() {
        System.out.println("Washing machine is washing clothes...");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        WashingMachine myWasher = new WashingMachine();  // Create an object of the WashingMachine class
        myWasher.turnOn();          // Call inherited method from Appliance class
        myWasher.washClothes();     // Call method specific to WashingMachine class
        myWasher.turnOff();         // Call inherited method from Appliance class
    }
}

How The Code Works

  • The Appliance class provides general methods for turning the appliance on and off (turnOn() and turnOff()).
  • The WashingMachine class extends Appliance and adds its own method washClothes(), which is specific to a washing machine.
  • In the main() method, when you create an object of WashingMachine, it can use both the inherited methods from Appliance and its own method washClothes().

Output

Appliance is now ON.
Washing machine is washing clothes...
Appliance is now OFF.

=== Code Execution Successful ===

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

In Java, when a subclass is created, its constructor automatically calls the parent class's constructor. It is done so that the parent class is correctly initialised before the subclass starts adding its own functionality. The super () keyword explicitly calls the parent class’s constructor or access its methods and fields.

Now let’s look at an example of super () and subclass constructors in single level inheritance in Java:

Algorithm

1. Start

2. Define a class Person

  • Declare a variable name
  • Create a constructor that takes name as a parameter
  • Assign name to the instance variable
  • Print "Person constructor called: Name is <name>"

3. Define a class Student that inherits from Person

  • Declare a variable grade
  • Create a constructor that takes name and grade as parameters
  • Call the super(name) constructor from Person class
  • Assign grade to the instance variable
  • Print "Student constructor called: Grade is <grade>"

4. Define the Main class. Inside the main() method:

  • Create an object student of class Student with values "Aarav" and 10
  • This triggers the constructor of Student, which first calls the constructor of Person
  • The Person constructor executes and prints the name
  • The Student constructor executes and prints the grade

5. End

Code

// Parent class
class Person {
    String name;

    // Constructor
    Person(String name) {
        this.name = name;
        System.out.println("Person constructor called: Name is " + name);
    }
}

// Child class
class Student extends Person {
    int grade;

    // Constructor
    Student(String name, int grade) {
        super(name); // Call to parent class constructor
        this.grade = grade;
        System.out.println("Student constructor called: Grade is " + grade);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Student student = new Student("Aarav", 10);  // Create a Student object
    }
}

How The Code Works

  • The Person class has a constructor that initialises the name field.
  • The Student class extends Person and adds a grade field.
  • Inside the Student constructor, super(name) is used to call the Person constructor so that the name is initialised first.

Output

Person constructor called: Name is Aarav
Student constructor called: Grade is 10

=== Code Execution Successful ===

Benefits of Single Inheritance in Java

  • Single inheritance helps maintain a clear hierarchy. Therefore, it makes the code easier to understand and manage.
  • The child class avoids redundant code by inheriting properties and methods from the parent class. Therefore, it avoids wasting time on development.
  • The child class can directly use the parent class's methods and fields. So it can save time and effort when writing repetitive code.
  • The child class can build on inherited properties by adding its unique methods and attributes. Therefore, it can offer a higher level of abstraction.

Limitations of Single Inheritance in Java

  • A class can inherit from only one parent class. Therefore, it limits the options for designing complex relationships or behaviours.
  • Not all code fits neatly into a single-inheritance hierarchy. It can reduce opportunities for reusing code effectively.
  • If inherited methods have the same names but perform different operations, it can lead to confusion or unexpected behaviour in the program.

Real-life Uses and Applications of Single Inheritance in Java

  1. Banking Systems: A SavingsAccount class can inherit from a BankAccount class. It reuses common banking functionalities.
  2. E-commerce Platforms: A Customer class can extend a User class. This is because it inherits login and profile management features.
  3. Educational Systems: A Student class can inherit from a Person class. Hence it can add student specific details like grades and courses.
  4. Vehicle Management: A Car class can extend a Vehicle class and reuse methods like start() and stop().
  5. Appliance Control: A WashingMachine class can inherit power control functionalities from an Appliance class.

Conclusion

Single inheritance in Java is an important concept that every student and beginner should learn. It lets you understand how to reuse code and keep your programs organised. For new people in coding, it’s a simple way to build a strong base in object-oriented programming. By learning single inheritance, you also prepare yourself for bigger topics like multilevel inheritance and polymorphism. To build coding skills, you must also take structured classes with tailored programs. The CCBP 4.0 Academy program is just what you need to build skills and get a leg up in your software career. 

Frequently Asked Questions

 1. What is single inheritance in Java?

Single inheritance in Java means a class (child class) inherits the properties and methods of another class (parent class). It’s like passing down the code from one generation to the next.

2. Why is single inheritance useful?

It makes coding easier by reusing code from the parent class. In a single inheritance in Java program, you don’t have to write the same code again and again. It also keeps your program organised.

3. Can a child class have more than one parent class in a single inheritance?

No, in single inheritance in Java, a class can only inherit from one parent class. If you need multiple parents, Java provides interfaces for that.

4. What is the role of the extends keyword in single inheritance?

The extends keyword is used to show that a class is inheriting from another class. For example, class Bird extends Animal means Bird is inheriting from Animal.

5. Can the child class add its own methods in single inheritance?

Yes, the child class can add its own methods and properties. Along with inheriting the parent class's features, it can have new ones, too!

Read More Articles

Chat with us
Chat with us
Talk to career expert