Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsObject-Oriented Programming
✓ FreeBeginner· 8 min read

Inheritance, Composition, this & super — Interview Questions

Inheritance and why Java forbids multiple class inheritance, composition over inheritance, association vs aggregation vs composition, IS-A vs HAS-A, and the this and super keywords.

Published September 25, 2026


How to use this lesson

Interviewers use inheritance questions to find out whether you'll build rigid class hierarchies or flexible designs. Show that you know the mechanics (extends, super), and when not to use inheritance.

Q1. What is inheritance in Java?

Short answer: Inheritance lets a class (the subclass) acquire the fields and methods of another class (the superclass) using extends. The subclass reuses and specialises that behaviour, and can override methods.

class Vehicle {
    protected int speed;
    void accelerate(int delta) { speed += delta; }
}
class Car extends Vehicle {               // Car IS-A Vehicle
    int doors = 4;
    @Override void accelerate(int delta) { speed += Math.min(delta, 20); }   // specialised behaviour
}

Key points to cover:

  • Java supports single class inheritance. A class extends exactly one class. If you write nothing, that class is Object.
  • Private members are inherited in memory but aren't accessible directly. Constructors are not inherited.
  • The types of inheritance are single, multilevel (A → B → C) and hierarchical (B and C both extend A). Multiple inheritance is possible only through interfaces.

Learn it in depth → Inheritance and Polymorphism

Q2. What is the purpose of inheritance?

Short answer: Code reuse, and above all substitutability: a Car can be used anywhere a Vehicle is expected. That's what makes polymorphism possible.

Key points to cover:

  • A good inheritance relationship obeys the Liskov Substitution Principle: a subclass must honour everything the parent promises. The classic bad example is Square extends Rectangle, because setting the width independently breaks the square.
  • Inheritance creates tight coupling. Subclasses depend on the parent's implementation details (the "fragile base class" problem).

Learn it in depth → Liskov Substitution & Interface Segregation

Q3. Can a class extend itself?

Short answer: No. class A extends A {} is a compile error ("cyclic inheritance involving A"). The same applies to longer cycles, such as A extends B with B extends A.

Q4. Why doesn't Java support multiple inheritance of classes?

Short answer: To avoid the diamond problem. If C extended both A and B, and both defined greet(), it would be ambiguous which one C inherits. There's also the harder question of how to combine two sets of inherited state. Java keeps class inheritance single, and gets multiple inheritance of type through interfaces.

Key points to cover:

  • Since Java 8, interfaces can have default methods, so a similar conflict can arise. Java resolves it by forcing the class to override the method, and optionally choose one version with A.super.greet().
  • Interfaces carry no instance state, which removes the hardest part of the problem.

Learn it in depth → Interfaces and Abstract Classes

Q5. What's the difference between inheritance and composition? What does "composition over inheritance" mean?

Short answer: Inheritance is an IS-A relationship: the subclass is a specialised parent. Composition is a HAS-A relationship: a class contains other objects and delegates work to them. "Composition over inheritance" means preferring to combine objects, because it's more flexible and less coupled.

// Inheritance misuse: a Car is not an Engine
// class Car extends Engine { }

// Composition: Car HAS-A Engine, and the engine can be swapped
class Car {
    private final Engine engine;                 // injected, so easy to replace or mock
    Car(Engine engine) { this.engine = engine; }
    void start() { engine.ignite(); }
}
interface Engine { void ignite(); }
class PetrolEngine implements Engine { public void ignite() { /* … */ } }
class ElectricEngine implements Engine { public void ignite() { /* … */ } }

Key points to cover:

  • Composition lets you change behaviour at runtime (swap the engine), avoids fragile base classes, and keeps classes small.
  • Use inheritance only for true IS-A relationships where the subclass honours the parent's contract, or when a framework requires it.
  • Many design patterns (Strategy, Decorator) are composition in practice. Spring's dependency injection is composition too.

Learn it in depth → Strategy Pattern

Q6. What's the difference between association, aggregation and composition?

Short answer: All three describe how objects are related, from loosest to tightest:

  • Association: a general "uses" or "knows" relationship. A Teacher teaches Students, and both live independently.
  • Aggregation: a weak HAS-A. The part can exist without the whole. A Department has Professors, who remain if the department closes.
  • Composition: a strong HAS-A. The part's lifecycle is owned by the whole. An Order has OrderLines, which make no sense without the order.

Key points to cover:

  • In code, composition usually means the whole creates its parts and never shares them. Aggregation usually means the parts are passed in, and can be shared.
  • In UML, aggregation is drawn with a hollow diamond and composition with a filled diamond.

Q7. Explain the IS-A and HAS-A relationships.

Short answer: IS-A is inheritance or interface implementation: Car extends Vehicle, so a Car IS-A Vehicle. HAS-A is composition: Car has an Engine field, so a Car HAS-A Engine.

Key points to cover:

  • A quick test: if the sentence "X is a Y" is always true, including every behaviour Y promises, inheritance may fit. Otherwise use HAS-A.
  • instanceof checks IS-A relationships at runtime.

Q8. What are the this and super keywords?

Short answer: this refers to the current object. super refers to the parent-class part of the current object. It's used to call the parent's constructor, or its version of an overridden method.

class Employee {
    protected String name;
    Employee(String name) { this.name = name; }            // this.name = the field, name = the parameter
    String describe() { return "Employee " + name; }
}
class Manager extends Employee {
    private final int reports;
    Manager(String name, int reports) {
        super(name);                                       // must be the first statement in the constructor
        this.reports = reports;
    }
    @Override String describe() {
        return super.describe() + " managing " + reports;  // reuse the parent's version
    }
}

Key points to cover:

  • Uses of this:
    • Resolve a field/parameter name clash.
    • Call another constructor with this(...), known as constructor chaining.
    • Pass the current object as an argument.
    • Return this for fluent APIs.
  • this(...) and super(...) must be the first statement, so a constructor can't contain both. (Java 25's "flexible constructor bodies" allow statements that don't use this before the call.)

Learn it in depth → Inheritance and Polymorphism

Q9. Can this be reassigned? What happens if you use super in a class with no explicit parent?

Short answer: this is effectively final. this = other; is a compile error. And every class except Object has a superclass (Object by default), so super.toString() or super() compiles fine in a class that doesn't write extends.

Common trap: some answer keys claim that using super in a class without a parent is a compile error. It isn't: the implicit parent is Object. The only place super has nothing to refer to is inside java.lang.Object itself.

Q10. Can this or super be used in a static method?

Short answer: No. Static methods belong to the class, not to any object, so there's no current instance for this or super to refer to. Using them is a compile error ("non-static variable this cannot be referenced from a static context").

Key points to cover:

  • To call instance methods from static main, create an object first: new App().run().

Q11. How does super relate to polymorphism?

Short answer: Polymorphism lets an overriding method replace the parent's behaviour. super.method() lets the override extend that behaviour instead of replacing it: run the parent logic, then add to it. At runtime, calls through a parent-type reference still dispatch to the subclass's override, and super is how that override reaches the original.

Key points to cover:

  • This is the basis of the Template Method style, and of the classic toString()/equals() overrides that call super.
  • super.method() is bound statically to the parent's implementation. It doesn't go through dynamic dispatch.

Learn it in depth → Template Method Pattern

Follow-up questions this topic invites — and their answers

Q: Are constructors inherited? A: No. Each class declares its own constructors. If a subclass constructor doesn't call super(...) explicitly, the compiler inserts super(). That fails to compile if the parent has no no-argument constructor.

Q: Are private members inherited? A: The private fields exist in the subclass object's memory, but the subclass can't access them directly. It has to go through protected or public methods. Private methods can't be overridden.

Q: What order do constructors run in, in a class hierarchy? A: From the top down. Object's constructor runs first, then each parent's, then the subclass's. Static initialisers run once, when each class is loaded, also from parent to child.

Q: How can you stop a class from being inherited? A: Declare it final (like String), give it only private constructors, or (since Java 17) make it sealed and list exactly which classes may extend it.

Previous

Classes, Objects, Packages & Access Modifiers — Interview Questions

Next

Polymorphism, Overloading & Overriding — Interview Questions

AI Tutor

Lesson: Inheritance, Composition, this & super — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.