Inheritance in Java
Expert View
According to my experience, inheritance is not just a concept β it's the blueprint of
design thinking in Object-Oriented Programming.
Think of building a modern manufacturing system: you donβt rebuild every vehicle from scratch β you start with a
Vehicle base class and then specialize into Car, Bus, or
Truck.
This principle of specialization and generalization forms the foundation of scalable software
and real-world factories alike.
Inheritance drives reusability, consistency, and runtime polymorphism β the
three pillars of robust design.
My advice to every student: *Experiment, visualize, and build mini hierarchies in Java.* Watch how a simple
class structure can model complex manufacturing lines at NBKRISTβs Innovation Labs we have in our college. π
π‘To illustrate the concepts of Inheritance as outlined in your syllabus, I have selected the
vehicle manufacturing domain. This domain provides clear and relatable entities that effectively map to each
concept. Having worked as a Software Engineer with international clients such as Ford and General Motors, I find
this domain both familiar and well-suited for conveying these ideas with practical clarity.!
1. Introduction
Inheritance allows a new class to derive properties and behavior from an existing class. Itβs like how every
new vehicle model inherits standard safety and design features from its parent blueprint in an automotive
factory.
class Vehicle {
void start() { System.out.println("NBKRIST Vehicle starting..."); }
}
class Car extends Vehicle {
void drive() { System.out.println("Car driving smoothly..."); }
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.start();
c.drive();
}
}
π‘ Pro Tip: Always design your base class to be as reusable as possible β just
like a universal vehicle chassis in manufacturing!
2. Process of Inheritance
In Java, inheritance is implemented using the extends keyword. The subclass (child) inherits
accessible members from the superclass (parent), forming a βis-aβ relationship.
class Vehicle {
int wheels = 4;
}
class Bus extends Vehicle {
void showInfo() {
System.out.println("NBKRIST Bus with " + wheels + " wheels.");
}
}
public class FactoryDemo {
public static void main(String[] args) {
Bus b = new Bus();
b.showInfo();
}
}
π§ Pro Tip: Before extending, ensure your parent class has generic properties
that make sense to all subclasses.
4. Universal Superclass β Object Class
Every class in Java implicitly inherits from the Object class β much like every machine in a
factory follows universal operational protocols. This class provides methods such as equals(),
toString(), and hashCode().
class Vehicle {
String model;
Vehicle(String model) { this.model = model; }
public String toString() { return "NBKRIST Vehicle Model: " + model; }
}
public class ObjectDemo {
public static void main(String[] args) {
Vehicle v = new Vehicle("Bus-XL");
System.out.println(v);
}
}
π Pro Tip: Override toString() to give meaningful representations
to your objects β especially in debugging and logging.
In Java, a superclass reference variable can hold a reference to any of its subclass objects. Since Object
is the universal superclass of all classes in Java, it can refer to an instance of any class, making it a
generic reference type within the languageβs inheritance hierarchy.
Object base= new Vehicle("BUS-XXL");//
correct
very high-level form of polymorphism!!! or Universal polymorphism through Object upcasting.
5. Multilevel Inheritance
Multilevel inheritance represents a layered evolution β for instance, a Bus inherits from
Vehicle, and a LuxuryBus inherits from Bus.
class Vehicle { void start(){ System.out.println("NBKRIST Vehicle starting..."); } }
class Bus extends Vehicle { void move(){ System.out.println("Bus moving..."); } }
class LuxuryBus extends Bus { void airCondition(){ System.out.println("AC running..."); } }
public class MultiLevelDemo {
public static void main(String[] args) {
LuxuryBus lb = new LuxuryBus();
lb.start(); lb.move(); lb.airCondition();
}
}
ποΈ Pro Tip: Use multilevel inheritance to gradually specialize your base
features rather than overloading one class.
6. Application of Keyword super
The super keyword lets a subclass access its superclass members β ideal for refining inherited
behavior or invoking the parent constructor.
class Vehicle {
Vehicle() { System.out.println("NBKRIST Vehicle created"); }
void start() { System.out.println("Vehicle starting..."); }
}
class Car extends Vehicle {
Car() { super(); System.out.println("Car ready for test drive!"); }
void start() {
super.start();
System.out.println("Car engine started successfully!");
}
}
public class SuperDemo {
public static void main(String[] args) {
new Car().start();
}
}
βοΈ
Pro Tip: Use
super() wisely to ensure the correct initialization
flow in complex hierarchies.
As the super keyword plays a super-important role in Java inheritance, Iβve developed a dedicated page with detailed explanations and examples for each use of super. Youβre encouraged to explore it after reading this page.
Become Super
7. Constructor Method and Inheritance
Constructors are not inherited but invoked in a sequence from parent to child using super().
This ensures proper initialization from general (Vehicle) to specific (Car).
class Vehicle { Vehicle() { System.out.println("NBKRIST Vehicle built"); } }
class Car extends Vehicle { Car() { System.out.println("Car assembled"); } }
class SportsCar extends Car { SportsCar() { System.out.println("SportsCar tuned for performance"); } }
public class ConstructorChain {
public static void main(String[] args) {
new SportsCar();
}
}
π Pro Tip: Always design constructor chains logically β from general
initialization to specialization.
π Key Takeaways
- Inheritance forms the backbone of reusable, maintainable Java systems.
- Real-world manufacturing models like Vehicles perfectly reflect βis-aβ relationships.
super ensures structured hierarchy communication between classes.
- Every Java class inherits from
Object, forming the universal base.
- Practice by building your own Vehicle hierarchy β make NBKRIST proud of your code!