🟣 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 methodeating
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(); // अपना methodeating
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 नहीं।