Explain the IS-A (inheritance) and Has-A (composition) relationships in Java.

IS-A and Has-A are two important relationships in Object-Oriented Programming that help model real-world relationships between classes. IS-A is achieved through inheritance, while Has-A is achieved through composition or aggregation.

Key Points

IS-A Relationship (Inheritance)

An IS-A relationship exists when a subclass is a type of its superclass.

Example

A Dog IS-A Animal.

class Animal {

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

class Dog extends Animal {

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

Here, Dog inherits the properties and behaviors of Animal because a Dog is an Animal.

Has-A Relationship (Composition)

A Has-A relationship exists when one class contains an object of another class.

Example

A Car HAS-A Engine.

class Engine {

    void start() {
        System.out.println("Engine Started");
    }
}

class Car {

    private Engine engine = new Engine();

    void startCar() {
        engine.start();
    }
}

Here, Car uses an Engine object because a Car has an Engine.

Comparison

IS-A Relationship

Has-A Relationship

Real-World Examples

IS-A

Has-A

Interview Tip

A concise interview answer is

IS-A represents inheritance, where a subclass is a specialized form of a superclass, such as Dog IS-A Animal. Has-A represents composition, where one class contains another class as a member, such as Car HAS-A Engine. In modern application design, Has-A relationships are generally preferred because they provide better flexibility and loose coupling.

Last updated: 16 July 2026

Previous question: What is the difference between association, aggregation, and composition in Java?

Next question: How does method overloading relate to polymorphism?

More Core Java - Level II interview questions

All Core Java - Level II questions