Nearby lessons
35 of 125Java - Inheritance
- What class and object mean, with real-life examples
- The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Constructors, this, super, static and final
- Method overloading and method overriding (with a comparison table)
- Abstract classes and interfaces (including the modern changes)
- Association, Aggregation and Composition
Inheritance is a core concept of the Java language. This lesson explains Containers in Java — Class, Abstract Class, Interface and The super Keyword with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Containers in Java — Class, Abstract Class, Interface
A container is a Java element that manages other elements (variables, methods, blocks) inside it. There are three containers:
| Container | Concrete methods? | Abstract methods? | Objects possible? | Sharability |
|---|---|---|---|---|
| Class | Yes | No | Yes | Less |
| Abstract class | Yes (also abstract) | Yes | No (only references) | Medium |
| Interface | No (classically) | Yes | No (only references) | Most |
Remember the rule: an interface with no abstract methods would be empty (this was true in the classic Java; from Java 8, interfaces can also carry default and static methods — see Chapter 25).
Class syntax — the valid and invalid declarations
The classic material tests which class declarations are legal. The rules:
- Top-level classes may be only public or default — private class A and protected class A are invalid.
- final class A, abstract class A are valid; static class A, synchronized class A, native class A are invalid at top level.
- Inner classes can be private/protected/static/abstract/final/strictfp.
- extends allows only one super class; implements allows many interfaces; extends must come before implements.
- A class cannot extend itself (cyclic inheritance error).
The super Keyword
`super` refers to the parent class. It is used to:
- Call the parent class constructor: super(10); — must be the first statement.
- Call the parent class method: super.display();.
- Access the parent class variable: super.x;.
Important fact: when a child object is created, the parent constructor runs first (automatically, even if you do not write super()).
- Class = blueprint; Object = real thing made from it (memory in heap).
- Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
- Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
- Constructor initialises the object; name = class name, no return type.
- this = current object; super = parent class; static = belongs to class; final = cannot change.
- abstract class cannot be instantiated; interface is a contract a class implements.
- Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).