Nearby lessons

35 of 125

Java - Inheritance

📌 What You Will Learn
  • 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:

ContainerConcrete methods?Abstract methods?Objects possible?Sharability
ClassYesNoYesLess
Abstract classYes (also abstract)YesNo (only references)Medium
InterfaceNo (classically)YesNo (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).
Example01
JCode Cell
1class A { } // valid
2public class A { } // valid
3private class A { } // INVALID
4class A extends B { } // valid (one super class)
5class A extends B, C { } // INVALID (multiple inheritance)
6class A implements I1, I2 { } // valid
7class A extends B implements I { } // valid (extends first)
8class A implements I extends B { } // INVALID (wrong order)

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()).

In simple words: When a child object is created, the parent constructor runs first — automatically. The parent part is always ready before the child part, even if you never write super().
Example02
JCode Cell
1class Animal { Animal() { System.out.println("Animal created"); } }
2 
3class Dog extends Animal {
4 Dog() {
5 super(); // calls Animal() constructor
6 System.out.println("Dog created");
7 }
8}
9 
10class Test {
11 public static void main(String[] args) {
12 Dog d = new Dog();
13 }
14}
Output
Animal created Dog created
📝 Key Takeaways
  • 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).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1