Java Single Inheritance: Learn Parent-Child Classes with Examples

0

Now that you understand the extends keyword, it is time to see the simplest form of class inheritance in Java: single inheritance.

Single inheritance occurs when one child class extends exactly one parent class. It creates a straightforward relationship: one parent provides common behavior, and one child inherits that behavior while adding its own specialized features.

Single inheritance means one child class directly inherits from one parent class.

Why Use Single Inheritance?

Suppose an application needs to represent employees. Every employee may have a name and a common work() operation, but a developer may also need a specialized writeCode() operation. Instead of duplicating the common employee behavior, the specialized class can inherit it.

class Employee {
    String name = "Amit";

    void work() {
        System.out.println("Employee is working");
    }
}

class Developer extends Employee {
    void writeCode() {
        System.out.println("Developer is writing code");
    }
}

Here, Employee is the parent class and Developer is the child class. Because Developer extends Employee, a developer object can use the accessible members inherited from Employee and its own members as well.

Visualizing the Relationship

Employee
    |
    | extends
    ↓
Developer

There is only one inheritance level in this example. That is what makes it single inheritance.

One parent → One direct child relationship is the simplest way to recognize single inheritance.

Creating and Using the Child Object

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

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

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();

        dog.eat();
        dog.bark();
    }
}

The object is created using the child class, Dog. It can call both eat(), inherited from Animal, and bark(), declared directly inside Dog.

What Does the Child Really Gain?

A child class can inherit accessible fields and methods from its parent. It can also introduce new members and, when the language rules allow it, override inherited methods to provide more specialized behavior.

Child Class Can Example
Use inherited members Call eat() from Animal
Add new members Define bark() in Dog
Override methods Provide a specialized implementation of an inherited method
Have its own state Declare additional fields specific to the child

Single Inheritance in a Real Application

Consider an online payment system. A general Payment class could contain common behavior such as validating a transaction. A CardPayment class could extend it and add card-specific processing.

class Payment {
    void validate() {
        System.out.println("Payment validated");
    }
}

class CardPayment extends Payment {
    void processCard() {
        System.out.println("Card payment processed");
    }
}

public class Main {
    public static void main(String[] args) {
        CardPayment payment = new CardPayment();

        payment.validate();
        payment.processCard();
    }
}

This design keeps common payment behavior in one place while allowing the child class to concentrate on its specialized responsibility. In a real project, however, inheritance should be chosen because the domain relationship makes sense, not merely because code can be reused.

Single Inheritance and Method Overriding

Single inheritance also provides the foundation for method overriding. A child class can replace an inherited method with a more specialized implementation.

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

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

The parent defines general behavior, while the child provides behavior that is more appropriate for its specific type. Method overriding becomes especially powerful when combined with runtime polymorphism.

Single Inheritance Does Not Mean Only One Child

Be careful with the wording. Single inheritance means that a class has only one direct parent. It does not mean that a parent class can have only one child.

class Vehicle {
}

class Car extends Vehicle {
}

class Bike extends Vehicle {
}

Both Car and Bike have one direct parent, so each class individually satisfies the rule of single inheritance. The overall structure is also an example of hierarchical inheritance, which will be covered separately.

Single inheritance describes the number of direct parent classes a child has. It does not limit how many child classes a parent can have.

Common Beginner Mistakes

  • Thinking single inheritance means one parent can have only one child.
  • Assuming private parent members can be accessed directly from the child.
  • Using inheritance solely for code reuse without checking whether the relationship is logically an is-a relationship.
  • Confusing single inheritance with multilevel or hierarchical inheritance.
  • Forgetting that the child can add its own behavior in addition to inherited behavior.

Single Inheritance vs Multilevel Inheritance

Type Structure Key Idea
Single Animal → Dog One direct parent for the child.
Multilevel Animal → Dog → Puppy Inheritance continues through multiple levels.

Interview Insight

If an interviewer asks, "What is single inheritance in Java?", a precise answer is: "Single inheritance is a form of inheritance in which a class directly extends one parent class. The child can reuse accessible inherited members, add new members, and potentially override inherited methods."

Final Takeaway

Single inheritance is the simplest inheritance structure in Java: one child class extends one parent class. It provides a clean way to share common behavior while allowing the child to specialize it. Once this one-level relationship is clear, the next natural step is to connect multiple inheritance levels and understand how a class can inherit through another class. That structure is called multilevel inheritance.

Post a Comment

0Comments
Post a Comment (0)