Basic of Inheritance

Inheritance allows a class to reuse and extend another class.

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}
Dog dog = new Dog();

dog.eat();  // inherited from Animal
dog.bark(); // Dog's own method

1. Inheritance Hierarchy

Every Java class ultimately inherits from Object.

Object
  ↓
Animal
  ↓
Dog

If you don't specify a superclass:

class Dog {
}

Java automatically treats it as:

class Dog extends Object {
}

A class can have only one direct superclass.


2. Why Use Inheritance?

Code Reuse

Reuse existing fields and methods.

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
}

Dog automatically gets eat().

Extensibility

A subclass can add its own features.

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

3. What Can a Subclass Do?

A subclass can:

Inherit members

class Animal {
    public int age;
}

class Dog extends Animal {
}

Dog d = new Dog();
d.age = 5;

public and protected members are inherited. Package-private members are accessible when the subclass is in the same package.

Add new members

class Dog extends Animal {
    String name;

    void bark() {
        System.out.println("Woof");
    }
}

Override instance methods

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Woof");
    }
}

Hide static methods

class Animal {
    static void info() {
        System.out.println("Animal");
    }
}

class Dog extends Animal {
    static void info() {
        System.out.println("Dog");
    }
}

Static methods are hidden, not overridden.

Call superclass constructor

Constructors are not inherited, but a subclass can call the parent constructor using super().

class Animal {
    Animal(String name) {
        System.out.println(name);
    }
}

class Dog extends Animal {
    Dog() {
        super("Bruno");
    }
}

super() must be the first statement in the constructor.


4. Private Members

Private members are not inherited.

class Animal {
    private int age = 10;

    public int getAge() {
        return age;
    }
}

class Dog extends Animal {
}

Dog cannot directly access:

age; // ERROR

But it can use:

Dog dog = new Dog();

dog.getAge(); // OK

So the parent can provide public or protected methods to access its private data.


5. Upcasting

A child object can be stored in a parent reference.

Dog dog = new Dog();

Animal animal = dog;

This is called upcasting and happens automatically.

Object obj = new Dog();

A Dog object is also an Animal and an Object.


6. Downcasting

Converting a parent reference back to a child reference requires an explicit cast.

Animal animal = new Dog();

Dog dog = (Dog) animal;

This is safe because the actual object is a Dog.

But:

Animal animal = new Animal();

Dog dog = (Dog) animal; // Runtime error

Use instanceof to check first:

if (animal instanceof Dog) {
    Dog dog = (Dog) animal;
}

7. Multiple Inheritance

Java does not allow a class to extend multiple classes.

❌ Not allowed:

class A {}
class B {}

class C extends A, B { // ERROR
}

A class can extend one class:

class C extends A {
}

But it can implement multiple interfaces:

interface Flyable {
}

interface Swimmable {
}

class Duck implements Flyable, Swimmable {
}

So:

One parent class + multiple interfaces.


Quick Revision

Concept Meaning
extends Creates class inheritance
Superclass Parent class
Subclass Child class
Object Topmost Java class
super() Calls parent constructor
super.method() Calls parent method
Overriding Redefining an instance method
Hiding Redefining a static method
Upcasting Child → Parent
Downcasting Parent → Child
instanceof Checks object's type
Private members Not inherited
Constructors Not inherited
Multiple classes ❌ Not allowed
Multiple interfaces ✅ Allowed

Easy Rule

Inheritance = "is-a" relationship.

Dog is an Animal
Car is a Vehicle
Student is a Person

And remember:

extends → one class
implements → multiple interfaces