Introduction
Inheritance hierarchy is the structure of classes connected through inheritance relationships, forming a tree-like structure.
Basic Hierarchy
Object (Root)
|
Animal
/ \
Dog Cat
/ \
Puppy Kitten
Simple Example
// Level 1
class Animal {
void eat() {
System.out.println("Eating");
}
}
// Level 2
class Mammal extends Animal {
void breathe() {
System.out.println("Breathing");
}
}
class Bird extends Animal {
void fly() {
System.out.println("Flying");
}
}
// Level 3
class Dog extends Mammal {
void bark() {
System.out.println("Barking");
}
}
class Cat extends Mammal {
void meow() {
System.out.println("Meowing");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // From Animal
dog.breathe(); // From Mammal
dog.bark(); // Own method
}
}
Real-World Example: Vehicle Hierarchy
// Root class
class Vehicle {
String brand;
int year;
void start() {
System.out.println(brand + " starting");
}
void stop() {
System.out.println(brand + " stopping");
}
}
// Second level
class LandVehicle extends Vehicle {
int wheels;
void drive() {
System.out.println("Driving on land");
}
}
class WaterVehicle extends Vehicle {
boolean hasSail;
void sail() {
System.out.println("Sailing on water");
}
}
// Third level
class Car extends LandVehicle {
int doors;
void openTrunk() {
System.out.println("Trunk opened");
}
}
class Bike extends LandVehicle {
boolean hasCarrier;
void ringBell() {
System.out.println("Ring ring!");
}
}
class Boat extends WaterVehicle {
int capacity;
void anchor() {
System.out.println("Anchored");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.brand = "Toyota";
car.wheels = 4;
car.doors = 4;
car.start(); // From Vehicle
car.drive(); // From LandVehicle
car.openTrunk(); // Own method
}
}
Employee Hierarchy
// Base class
class Employee {
protected String name;
protected String employeeId;
protected double salary;
Employee(String name, String employeeId, double salary) {
this.name = name;
this.employeeId = employeeId;
this.salary = salary;
}
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("ID: " + employeeId);
System.out.println("Salary: " + salary);
}
}
// Middle level
class Manager extends Employee {
protected String department;
Manager(String name, String employeeId, double salary, String department) {
super(name, employeeId, salary);
this.department = department;
}
void manage() {
System.out.println(name + " manages " + department);
}
}
class Developer extends Employee {
protected String programmingLanguage;
Developer(String name, String employeeId, double salary, String language) {
super(name, employeeId, salary);
this.programmingLanguage = language;
}
void code() {
System.out.println(name + " codes in " + programmingLanguage);
}
}
// Third level
class TeamLead extends Manager {
private int teamSize;
TeamLead(String name, String employeeId, double salary,
String department, int teamSize) {
super(name, employeeId, salary, department);
this.teamSize = teamSize;
}
void conductMeeting() {
System.out.println(name + " conducting meeting with team of " + teamSize);
}
}
class SeniorDeveloper extends Developer {
private int experience;
SeniorDeveloper(String name, String employeeId, double salary,
String language, int experience) {
super(name, employeeId, salary, language);
this.experience = experience;
}
void mentor() {
System.out.println(name + " mentoring (" + experience + " years exp)");
}
}
Shape Hierarchy
// Root
class Shape {
String color;
Shape(String color) {
this.color = color;
}
double area() {
return 0;
}
void display() {
System.out.println("Color: " + color);
System.out.println("Area: " + area());
}
}
// Level 2
class Circle extends Shape {
double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
}
class Polygon extends Shape {
int sides;
Polygon(String color, int sides) {
super(color);
this.sides = sides;
}
}
// Level 3
class Rectangle extends Polygon {
double length, width;
Rectangle(String color, double length, double width) {
super(color, 4);
this.length = length;
this.width = width;
}
@Override
double area() {
return length * width;
}
}
class Triangle extends Polygon {
double base, height;
Triangle(String color, double base, double height) {
super(color, 3);
this.base = base;
this.height = height;
}
@Override
double area() {
return 0.5 * base * height;
}
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle("Red", 5);
c.display();
Rectangle r = new Rectangle("Blue", 4, 6);
r.display();
Triangle t = new Triangle("Green", 4, 5);
t.display();
}
}
Hierarchy Diagram
Object
|
Vehicle
/ \
LandVehicle WaterVehicle
/ \ |
Car Bike Boat
|
SportsCar
Benefits of Hierarchies
- Code Reusability: Inherit common features
- Organization: Logical structure
- Maintainability: Changes in one place
- Extensibility: Easy to add new classes
- Polymorphism: Treat objects uniformly
Design Considerations
Good Hierarchy:
// General to specific
Animal → Mammal → Dog → Labrador
Bad Hierarchy:
// Unrelated inheritance
Car extends Person // ✗ Bad design
IS-A Test:
- Dog IS-A Mammal ✓
- Mammal IS-A Animal ✓
- Car IS-A Vehicle ✓
- Car IS-A Person ✗ (Bad)
Deep vs Wide Hierarchies
Deep Hierarchy:
Object → A → B → C → D → E
Problem: Too many levels, hard to maintain
Wide Hierarchy:
Object
/ | | | \
A B C D E
Problem: No code reuse between siblings
Balanced (Best):
Object
/ \
A B
/ \ / \
C D E F
Multiple Inheritance (Not in Java)
Parent1 Parent2
\ /
Child
Java doesn’t support this with classes!
Use interfaces instead:
interface Flyable { }
interface Swimmable { }
class Duck implements Flyable, Swimmable {
// Can implement multiple interfaces
}
Accessing Hierarchy Members
class A {
void methodA() { }
}
class B extends A {
void methodB() { }
}
class C extends B {
void methodC() {
methodA(); // From A
methodB(); // From B
}
}
Polymorphism in Hierarchies
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal[] animals = new Animal[3];
animals[0] = new Animal();
animals[1] = new Dog();
animals[2] = new Cat();
for (Animal a : animals) {
a.sound(); // Polymorphic call
}
}
}
Output:
Animal sound
Bark
Meow
Common Hierarchy Patterns
1. Specialization:
Vehicle → Car → SportsCar
(General → Specific)
2. Categorization:
Animal
/ | \
Dog Cat Bird
3. Composition:
Employee
/ \
Manager Developer
| |
TeamLead SeniorDev
Complete University Example
// Base
class Person {
protected String name;
protected int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// Level 2
class Student extends Person {
protected int rollNo;
Student(String name, int age, int rollNo) {
super(name, age);
this.rollNo = rollNo;
}
}
class Staff extends Person {
protected String employeeId;
Staff(String name, int age, String employeeId) {
super(name, age);
this.employeeId = employeeId;
}
}
// Level 3
class UndergraduateStudent extends Student {
String course;
UndergraduateStudent(String name, int age, int rollNo, String course) {
super(name, age, rollNo);
this.course = course;
}
}
class PostgraduateStudent extends Student {
String researchTopic;
PostgraduateStudent(String name, int age, int rollNo, String topic) {
super(name, age, rollNo);
this.researchTopic = topic;
}
}
class Teacher extends Staff {
String subject;
Teacher(String name, int age, String employeeId, String subject) {
super(name, age, employeeId);
this.subject = subject;
}
}
class Admin extends Staff {
String department;
Admin(String name, int age, String employeeId, String department) {
super(name, age, employeeId);
this.department = department;
}
}
Quick Reference
Hierarchy Structure:
Root
/ \
Child1 Child2
| |
SubChild1 SubChild2
- Root: Most general
- Child: Specialized
- SubChild: More specialized
All inherit from Object (automatically)
Exam Tips
Remember:
- Hierarchy = tree structure of classes
- Root at top (most general)
- Leaves at bottom (most specific)
- All Java classes inherit from Object
- Use IS-A test for validity
- Single inheritance only
- Each level adds specialization
- Promotes code reuse
- Enables polymorphism
- Keep hierarchies balanced
Common Questions:
- What is inheritance hierarchy?
- How to design hierarchy?
- Benefits of hierarchies?
- What is Object class?
- Deep vs wide hierarchy?
- IS-A relationship?
- Multiple inheritance in Java?