1. Introduction
Inheritance is an OOP mechanism that allows one class (the subclass or child class) to acquire the fields and methods of another class (the superclass or parent class), using the extends keyword. It promotes code reuse and establishes an 'is-a' relationship between classes.
Cricket analogy: Inheritance is like a young all-rounder joining a franchise's academy and automatically picking up the team's core training methods and playbook via extends, establishing that he 'is-a' member of that franchise's system while adding his own signature shots.
In Java, every class implicitly extends java.lang.Object if it does not explicitly extend another class, making Object the root of the entire class hierarchy. This means every Java object automatically inherits methods like toString(), equals(), and hashCode().
Cricket analogy: Every Java class implicitly extends Object, like every cricketer implicitly belonging to the ICC's foundational rules even without stating it, automatically inheriting baseline behaviors — like how every player automatically has a standard scorecard entry (toString()), a way to be compared to another player (equals()), and a unique registration number (hashCode()).
2. Syntax
class Parent {
// parent fields and methods
}
class Child extends Parent {
// Child inherits Parent's non-private members
// and can add its own fields/methods
Child() {
super(); // calls Parent's constructor (implicit if omitted)
}
}3. Explanation
A subclass inherits all non-private fields and methods of its superclass and can add new members or override existing methods to provide specialized behavior. The super keyword is used to explicitly access the parent class's fields, methods, or constructor from within the subclass.
Cricket analogy: A subclass like FastBowler inherits non-private fields and methods from the Bowler superclass but can override bowl() with a yorker specialty, and use super.bowl() to still fall back on the parent's standard delivery technique when needed.
Critical exam point: Java does NOT support multiple inheritance of classes — a class can extend only ONE superclass (class C extends A, B is illegal). However, Java DOES support multiple inheritance of type via interfaces — a class can implement multiple interfaces. This avoids the 'diamond problem' associated with multiple class inheritance.
Every class in Java, if it doesn't extend another class explicitly, implicitly extends java.lang.Object — making Object the ultimate root of every class hierarchy.
4. Example
public class Vehicle {
String brand = "Generic";
void honk() {
System.out.println("Beep beep!");
}
}
class Car extends Vehicle {
int wheels = 4;
void display() {
System.out.println("Brand: " + super.brand + ", Wheels: " + wheels);
super.honk(); // calling parent's method explicitly
}
}
class Demo {
public static void main(String[] args) {
Car myCar = new Car();
myCar.display();
myCar.honk(); // inherited method usable directly too
}
}5. Output
Brand: Generic, Wheels: 4
Beep beep!
Beep beep!6. Key Takeaways
- Inheritance lets a subclass reuse fields and methods of a superclass via
extends. - Java supports only single inheritance for classes — a class can extend just one superclass.
- Multiple inheritance of type is possible in Java only through interfaces, not classes.
- The
superkeyword accesses the parent class's fields, methods, or constructor. - Every class implicitly extends java.lang.Object if no other superclass is specified.
Practice what you learned
1. Which keyword is used for class inheritance in Java?
2. Does Java support multiple inheritance of classes?
3. What is the root class of every class hierarchy in Java?
4. What is the purpose of the `super` keyword?
5. In the example, what does myCar.display() print for the brand?
Was this page helpful?
You May Also Like
Polymorphism in Java
Explore compile-time and runtime polymorphism in Java, including method overloading, overriding, and dynamic dispatch.
this Keyword in Java
Master the `this` keyword in Java — resolving field/parameter name conflicts, constructor chaining, and passing the current object.
Abstraction in Java
Understand abstraction in Java through abstract classes and interfaces, and how they hide implementation details from the user.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics