Java Inheritance

🔹 Java Inheritance

Inheritance in Java is an OOP concept where a class (child/subclass) acquires the properties and methods of another class (parent/superclass).

It promotes code reusability and establishes a hierarchical relationship between classes.


✅ 1. Types of Inheritance in Java

Type Description
Single Inheritance One child inherits from one parent
Multilevel Inheritance Class inherits from another class, which inherits from another class
Hierarchical Inheritance Multiple child classes inherit from one parent class
Multiple Inheritance (via Interfaces) Java does not support multiple class inheritance directly but supports it using interfaces

✅ 2. Syntax

class Parent {
// Parent class members
}

class Child extends Parent {
// Child class inherits Parent members
}

  • extends keyword is used for inheritance.

  • Child class inherits all accessible members of parent class.


✅ 3. Example 1: Single Inheritance

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

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

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited from Animal
d.bark(); // own method
}
}

📌 Output:

Animal is eating
Dog is barking

✅ 4. Example 2: Multilevel Inheritance

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

class Mammal extends Animal {
void sleep() { System.out.println("Mammal is sleeping"); }
}

class Dog extends Mammal {
void bark() { System.out.println("Dog is barking"); }
}

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // from Animal
d.sleep(); // from Mammal
d.bark(); // own method
}
}

📌 Output:

Animal is eating
Mammal is sleeping
Dog is barking

✅ 5. Example 3: Hierarchical Inheritance

class Vehicle {
void start() { System.out.println("Vehicle started"); }
}

class Car extends Vehicle {
void drive() { System.out.println("Car is driving"); }
}

class Bike extends Vehicle {
void ride() { System.out.println("Bike is riding"); }
}

public class Main {
public static void main(String[] args) {
Car c = new Car();
Bike b = new Bike();

c.start();
c.drive();

b.start();
b.ride();
}
}

📌 Output:

Vehicle started
Car is driving
Vehicle started
Bike is riding

✅ 6. Key Points About Inheritance

  • Use extends keyword to inherit from a class.

  • Private members of parent class are not inherited.

  • Promotes code reusability and hierarchy.

  • Constructors are not inherited, but child can call parent constructor using super.

  • Java does not support multiple class inheritance to avoid ambiguity (use interfaces instead).

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *