Inheritance in Java: Syntax, Example Program and Key Rules

Inheritance in Java is an object-oriented programming mechanism where a subclass acquires the fields and methods of a superclass using the extends keyword. It models an IS-A relationship — for example, a Car IS-A Vehicle — enabling code reuse and a logical class hierarchy without rewriting shared behaviour in every new class.

Key Takeaways

  • Java inheritance uses the extends keyword to link a subclass to a superclass.
  • A subclass inherits all public and protected members of its superclass, but not private ones.
  • Inheritance models an IS-A relationship: a Car IS-A Vehicle, a Dog IS-A Animal.
  • Java does not support multiple inheritance through classes, only through interfaces.
  • Every Java class implicitly extends java.lang.Object if no other superclass is specified.

What Inheritance in Java Means

The inheritance definition in Java is straightforward: it is a mechanism where a new class is built on top of an existing class, inheriting its state and behaviour. You avoid duplicating code, and you create a logical hierarchy that mirrors the real world.

Think of a Vehicle class that stores brand and speed, and has a method move(). A Car class needs all of that too, plus a numberOfDoors field specific to cars. Without inheritance in Java, you would copy everything from Vehicle into Car. With inheritance, Car simply extends Vehicle and adds only what is unique to it.

The IS-A Relationship

Inheritance in Java represents an IS-A relationship. A Car IS-A Vehicle. A Bike IS-A Vehicle. This distinction matters when you are designing a class hierarchy because not every relationship between objects is an IS-A. A Car HAS-A Engine, so Engine should be a field inside Car, not a superclass of it. Getting this wrong is one of the most common beginner mistakes in Java OOP.

If you are studying BSc Computer Science subjects, OOP and inheritance will appear in almost every semester that touches programming. It is a concept that follows you from your first Java lab all the way into enterprise software development.

Why Use Inheritance in Java?

Inheritance cuts down on redundant code. According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, partly because its OOP model, including inheritance, makes large codebases manageable. The TIOBE Index (April 2025) places Java consistently in the top four languages worldwide. Additionally, the JetBrains Developer Ecosystem Survey 2024 found that 62% of Java developers cite object-oriented design patterns, including inheritance and polymorphism, as skills they use daily in production code.

Beyond code reuse, inheritance makes your programs easier to extend. You can add a new subclass, say Truck, without touching the existing Vehicle or Car classes. That is the open-closed principle in action, and it is exactly how real-world software teams build scalable systems.

Types of Inheritance in Java

Java supports several forms of inheritance in Java, each with different class structures. Understanding the types is a common exam and interview topic.

Type Classes Involved Supported via Classes Example
Single One parent, one child Yes Car extends Vehicle
Multilevel Grandparent, parent, child Yes ElectricCar extends Car extends Vehicle
Hierarchical One parent, multiple children Yes Car extends Vehicle; Bike extends Vehicle
Multiple Two or more parents, one child No (classes); Yes (interfaces) class C implements A, B
Hybrid Combination of above types No (classes); Yes (interfaces) Achieved via interface chaining

Java deliberately blocks multiple inheritance through classes to avoid the diamond problem, where two parent classes define the same method and the compiler cannot decide which version to use. Interfaces solve this cleanly because they define contracts, not implementations (prior to default methods in Java 8).

Syntax: extends, Superclass and Subclass

The inheritance syntax in Java is minimal and readable. You declare a subclass using the extends keyword followed by the superclass name.

How the extends Keyword Works

When you write class Car extends Vehicle, the Java compiler sets up a parent-child link at the bytecode level. The Car object in memory contains all the fields from Vehicle plus its own. You do not need to redeclare them.

The super keyword is your bridge back to the superclass. Inside a Car constructor, calling super(brand, speed) passes values up to Vehicle’s constructor. If you forget this and Vehicle has no no-argument constructor, your code will not compile. That is a very common runtime surprise for beginners, and you will want to understand Java exceptions and common runtime errors to debug it confidently.

Access Modifiers and Inheritance in Java

Not everything in a superclass is visible to a subclass. Here is what each access level means for inherited members:

  • public: Inherited and accessible from anywhere.
  • protected: Inherited and accessible within the subclass and the same package.
  • default (no modifier): Accessible only within the same package, not across packages even in a subclass.
  • private: Not inherited. The field or method exists in the object’s memory, but the subclass cannot directly read or write it.

This is one of the rules that trips up students in university exams and job interviews alike. A private field in Vehicle, say private int engineNumber, is part of every Car object but Car’s code cannot touch it directly. You would need a public getter or setter in Vehicle to access it from Car.

Simple Inheritance in Java Program Example

Here is a complete, runnable example using the Vehicle and Car model. Read through the comments to understand what each line does.


// Superclass
class Vehicle {
    String brand;
    int speed;

    Vehicle(String brand, int speed) {
        this.brand = brand;
        this.speed = speed;
    }

    void move() {
        System.out.println(brand + " is moving at " + speed + " km/h.");
    }
}

// Subclass (child class)
class Car extends Vehicle {
    int numberOfDoors;

    Car(String brand, int speed, int numberOfDoors) {
        super(brand, speed);   // calls Vehicle's constructor
        this.numberOfDoors = numberOfDoors;
    }

    void displayDetails() {
        System.out.println("Brand: " + brand + ", Doors: " + numberOfDoors);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Tata Nexon", 120, 5);
        myCar.move();           // inherited from Vehicle
        myCar.displayDetails(); // defined in Car
    }
}

Output:


Tata Nexon is moving at 120 km/h.
Brand: Tata Nexon, Doors: 5

Notice that myCar.move() works even though move() is defined in Vehicle, not in Car. That is inheritance in Java in action. The Car object carries the full Vehicle behaviour without you writing a single extra line for it.

If you are new to compiling and running programs and want to understand the process from scratch, check out our guide on how to write, compile and run a C program to get comfortable with the command-line workflow before you scale up to Java projects.

What Happens at Runtime

When new Car(...) is called, the JVM first runs Vehicle’s constructor (via super()), then finishes Car’s constructor. This order is always superclass first, then subclass. If you override move() in Car, the JVM uses Car’s version at runtime, which is how polymorphism connects to inheritance in Java, though that is a deeper topic on its own.

Rules and Best Practices for Inheritance in Java

Java class inheritance comes with a specific set of rules you need to know cold, especially for exams and technical interviews.

Core Rules of Java Inheritance

  1. Single inheritance only: A Java class can extend only one superclass. class C extends A, B is a compile error.
  2. All classes extend Object: If you do not write extends, Java automatically makes your class extend java.lang.Object. That is why methods like toString() and equals() are available on every Java object.
  3. Constructors are not inherited: The subclass must define its own constructor. It can call the superclass constructor using super(), but it does not inherit the constructor itself.
  4. Private members are not accessible: They exist in the inherited object but cannot be directly referenced in the subclass code.
  5. The final keyword blocks inheritance: A class declared final cannot be extended. java.lang.String is a classic example from the Oracle Java SE documentation.

Practical Tips from Real Development

In production Java code, teams rarely build deep inheritance chains of four or five levels. The deeper the hierarchy, the harder it is to trace where a bug originates. Two or three levels is generally the practical limit before composition becomes a better design choice.

One thing you will see in real codebases: superclass methods are often marked protected rather than public when they are only meant for subclasses to use, not for external callers. It is a small discipline that prevents a lot of accidental misuse. If you are considering a career as a full stack developer vs software developer, knowing when to use inheritance versus composition is exactly the kind of design judgment that separates junior from mid-level engineers.

Common Mistakes to Avoid

  • Using inheritance when composition is more appropriate. If the relationship is HAS-A, use a field, not extends.
  • Forgetting super() in the subclass constructor when the superclass has no default constructor.
  • Trying to override a private method. It compiles, but you are actually creating a new method, not overriding anything.
  • Assuming constructors are inherited. They are not, and this catches students out in every Java exam.

Strong OOP skills, including inheritance in Java, are foundational for roles in software engineering, Android development, and secure coding. According to NASSCOM’s Tech Talent Report 2024, Java remains the most in-demand programming language for software development roles in India, with OOP proficiency listed as a mandatory skill in over 70% of Java job descriptions on major Indian hiring platforms. Students exploring jobs and salary after MCA will find Java OOP listed as a core requirement across most software development job descriptions in India.

If you are building your programming toolkit from scratch, our guide on software development tools to learn first will help you set up the right environment before you write your first Java class.

Frequently Asked Questions

What is inheritance in Java?

Inheritance in Java is an OOP mechanism where a subclass acquires the fields and methods of a superclass using the extends keyword. It models an IS-A relationship, for example, a Car IS-A Vehicle. This allows code reuse and a logical class hierarchy without rewriting shared behaviour in every new class.

What are the types of inheritance in Java?

Java supports single, multilevel, and hierarchical inheritance through classes. Multiple inheritance and hybrid inheritance are supported only through interfaces, not classes. Java blocks multiple class inheritance to avoid the diamond problem, where two parent classes define the same method and the compiler cannot resolve which version to use.

What is the syntax of inheritance in Java?

The syntax is class SubclassName extends SuperclassName { }. Inside the subclass constructor, you call super() to invoke the superclass constructor. The extends keyword is the only mechanism for class-level inheritance in Java, and each class can extend only one superclass.

What is a child class in Java?

A child class (also called a subclass) is a class that extends another class and inherits its public and protected members. In the example above, Car is the child class and Vehicle is the parent. The child class can add its own fields and methods, and it can also override methods from the parent class.

Can a subclass access private members of the superclass?

No. Private fields and methods in a superclass are not directly accessible in a subclass, even though they exist in the object’s memory. To read or modify a private field from a subclass, the superclass must provide a public or protected getter and setter method. This is standard encapsulation practice in Java.

Why use inheritance in Java?

Inheritance in Java reduces code duplication by letting subclasses reuse superclass behaviour. It makes programs easier to extend because you can add a new subclass without modifying existing code. It also enables polymorphism, where a superclass reference can point to a subclass object, making code more flexible and scalable in real-world applications.

You now have the full picture of inheritance in Java: what it means, the types available, how to write the syntax, a working program you can run today, and the rules that matter most. Here are three things to do this week:

  1. Type out the Vehicle-Car example by hand in your IDE, run it, then add a third class like Truck that also extends Vehicle and has a payloadCapacity field.
  2. Experiment with access modifiers: change brand to private in Vehicle, see the compile error in Car, then fix it using a getter method.
  3. Explore the 3.0 University online certification courses to build on these fundamentals. Inheritance, secure coding and OOP design principles are directly relevant to cybersecurity and ethical hacking, and if that career path interests you, the CEH v13 AI online course is a strong next step for turning programming knowledge into a recognised security credential.

Last updated: May 2025. Reviewed by the 3University editorial team.

We create, review, and maintain educational content covering Cybersecurity, Ethical Hacking, Artificial Intelligence, Blockchain, Web3, and emerging technologies.

Read More Articles