Java

Inheritance in Java – How Inheritance Works with Examples & Interview Questions

Inheritance in Java 🚀

By Bhau Automation • Java Tutorial for Beginners

🎯 What You Will Learn

  • What is inheritance in Java?
  • How inheritance works in Java
  • Types of inheritance
  • Real Java inheritance examples
  • Java interview questions on inheritance
💡 Inheritance is one of the most important OOPs concepts in Java programming.

📌 What is Inheritance in Java?

Inheritance is a mechanism in Java where one class acquires the properties and methods of another class.

Inheritance helps in:

  • Code reusability
  • Reducing duplicate code
  • Method overriding
  • Improving maintainability

🛠️ Syntax of Inheritance

class Parent {

}

class Child extends Parent {

}

⚙️ How Inheritance Works?

The child class inherits properties and methods from the parent class using the extends keyword.

💻 Java Inheritance Example

class Animal {

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

class Dog extends Animal {

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

public class InheritanceDemo {

    public static void main(String[] args) {

        Dog d = new Dog();

        d.sound();

        d.bark();
    }
}

📚 Types of Inheritance in Java

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance (through interfaces)

🌐 Real-World Example

A Car class can inherit properties from a Vehicle class.

  • Vehicle → speed()
  • Car → musicSystem()

🔥 Advantages of Inheritance

  • Code reusability
  • Cleaner code structure
  • Supports polymorphism
  • Easy maintenance

❓ Common Interview Questions

Q: Which keyword is used for inheritance in Java?

A: The extends keyword is used.

Q: Does Java support multiple inheritance using classes?

A: No, Java supports multiple inheritance through interfaces only.

🎥 Watch Complete Video Tutorial

👉 Watch Inheritance in Java Tutorial

🎓 Key Takeaways

  • Inheritance promotes code reusability
  • Child class inherits parent class features
  • extends keyword is used in Java inheritance
  • Important OOPs concept for interviews
  • Foundation for advanced Java concepts
Pro Tip: Practice inheritance programs regularly to understand OOPs concepts deeply for interviews and development projects.

🚀 Created with ❤️ by Bhau Automation