🟣 OOP  ·  Lesson 19

Inheritance

Inheritance

What is Inheritance?

Inheritance lets one class (child) reuse the fields and methods of another (parent) using the extends keyword.

Example

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

Dog d = new Dog();
d.eat();   // inherited from Animal
d.bark();  // own method
eating barking

Types of Inheritance

Java supports single, multilevel and hierarchical inheritance. It does NOT support multiple inheritance with classes (uses interfaces instead).

Summary

  • Inheritance reuses parent code via extends.
  • Java: single, multilevel, hierarchical; no multiple inheritance with classes.

Inheritance क्या है?

Inheritance एक class (child) को दूसरी (parent) के fields और methods extends keyword से reuse करने देता है।

Example

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

Dog d = new Dog();
d.eat();   // Animal से inherited
d.bark();  // अपना method
eating barking

Inheritance के प्रकार

Java single, multilevel और hierarchical inheritance support करता है। यह classes के साथ multiple inheritance support नहीं करता (इसके बजाय interfaces use करता है)।

सारांश

  • Inheritance extends से parent code reuse करता है।
  • Java: single, multilevel, hierarchical; classes के साथ multiple नहीं।
← Back to Java Tutorial
🔗

Share this topic with a friend

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

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

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

\n