Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 19

Inheritance in Java

In this lesson: Learn single, multilevel and hierarchical inheritance in Java with extends, super and examples with output. In Hindi & English.

What is inheritance

Inheritance lets one class reuse the fields and methods of another. The child class inherits from a parent class using extends, getting everything the parent has plus its own additions. This avoids repeating shared code and models "is-a" relationships (a Dog is an Animal).

💡 class Dog extends Animal means Dog inherits everything from Animal.

Using extends

class Animal {
    void eat() { System.out.println("eating"); }
}
class Dog extends Animal {
    void bark() { System.out.println("woof"); }
}
// use:
Dog d = new Dog();
d.eat();    // inherited from Animal
d.bark();   // its own
eating
woof

Dog extends Animal gives Dog the eat() method for free, and Dog adds its own bark(). One keyword unlocks all the parent's abilities.

Overriding a method

class Animal {
    void sound() { System.out.println("Some sound"); }
}
class Cat extends Animal {
    void sound() {          // override
        System.out.println("Meow");
    }
}
// use:
new Cat().sound();
Meow

A child can replace an inherited method by defining its own with the same name — this is overriding. For a Cat object, the child's sound() runs instead of the parent's.

super in inheritance

class Person {
    Person(String name) { System.out.println("Person: " + name); }
}
class Student extends Person {
    Student(String name) {
        super(name);        // call parent constructor
        System.out.println("Student created");
    }
}
new Student("Aman");
Person: Aman
Student created

super(name) calls the parent's constructor first, then the child continues. super also calls a parent's overridden method with super.method().

Types of inheritance

  • Single: one child, one parent (Dog → Animal).
  • Multilevel: a chain (Puppy → Dog → Animal).
  • Hierarchical: many children of one parent (Dog and Cat both → Animal).

Java supports these with classes. It does not support multiple inheritance of classes (one child, many parents).

Why no multiple inheritance

Java does not let a class extend two classes at once, to avoid the diamond problem: if both parents had a method with the same name, the child could not tell which to use. To combine behaviour from multiple sources, Java uses interfaces, which sidestep this ambiguity.

Common mistakes

  • Wrong parent name. extends Animal needs Animal to exist and be spelled correctly.
  • Overriding by accident. Reusing a parent method name replaces it — rename if unintended.
  • Forgetting super(...) for parent setup. A parameterised parent constructor must be called.
  • Trying to extend two classes. Not allowed — use interfaces instead.

Summary

  • Inheritance reuses a parent's fields and methods via extends.
  • A child gets the parent's members plus its own.
  • Overriding replaces an inherited method with the same name.
  • super calls the parent's constructor or overridden method.
  • Java allows single, multilevel and hierarchical — not multiple inheritance of classes.

Next, use this and super in depth, or combine behaviour with abstraction and interfaces.

Frequently Asked Questions

What is inheritance in Java?
Inheritance lets a class (child) reuse the fields and methods of another class (parent) using the extends keyword. The child gets everything the parent has, plus anything it adds, which avoids repeating code.
What is method overriding?
Overriding is when a child class provides its own version of a method that the parent already has, using the same name and parameters. For a child object, the child's version runs instead of the parent's.
What are the types of inheritance in Java?
Java supports single (one parent), multilevel (a chain: A → B → C), and hierarchical (many children of one parent). It does not support multiple inheritance of classes, but achieves similar goals with interfaces.
Why does Java not allow multiple inheritance of classes?
To avoid the "diamond problem", where two parents have the same method and the child cannot decide which to use. Java avoids this ambiguity for classes, and uses interfaces to combine behaviour instead.
What does the super keyword do in inheritance?
super lets a child access its parent — calling the parent's constructor with super(...), or the parent's overridden method with super.method(). It connects the child back to the parent it extends.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPolymorphism Your progress: 19 / 61 (31%) · Lesson 20 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free