What is inheritance in Java and its types
๐ก Concept: Inheritance in Java
Inheritance is an OOP principle where a new class derives properties and behavior from an existing class, promoting code reuse.
๐ Quick Intro
Java supports single inheritance, where one class inherits from another, enabling hierarchical class structures.
๐ง Analogy
Think of inheritance like inheriting traits from your parents โ you get characteristics but can also have your own unique features.
๐ง Technical Explanation
- Single inheritance: one class extends another.
- Java does not support multiple inheritance with classes but allows it with interfaces.
- Types include single, multilevel, and hierarchical inheritance.
- Derived classes override or extend base class methods.
- Supports code reuse, polymorphism, and method overriding.
๐ฏ Use Cases
- โ Use inheritance to model "is-a" relationships.
- โ Extend existing classes to add or modify behavior.
- โ Promote code reuse and simplify maintenance.
๐ป Java Inheritance Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Output: Dog barks
}
}

โ Interview Q&A
Q1: What is inheritance?
A: Mechanism where one class acquires properties of another.
Q2: Does Java support multiple inheritance?
A: Not with classes, but yes with interfaces.
Q3: What are types of inheritance in Java?
A: Single, multilevel, hierarchical.
Q4: What is method overriding?
A: Subclass provides specific implementation of superclass method.
Q5: What is the use of "super" keyword?
A: To refer to parent class members.
Q6: Can private members be inherited?
A: No.
Q7: What is polymorphism?
A: Ability of objects to take many forms.
Q8: Can constructors be inherited?
A: No.
Q9: What is the difference between extends and implements?
A: Extends is for classes, implements for interfaces.
Q10: How does inheritance promote code reuse?
A: By sharing properties and methods.
๐ MCQs
Q1. What is inheritance in Java?
- Encapsulation
- Polymorphism
- Mechanism to acquire properties from another class
- Abstraction
Q2. Does Java support multiple inheritance?
- Yes
- No
- No with classes, yes with interfaces
- Only with classes
Q3. What are types of inheritance?
- Single only
- Multiple
- Single, multilevel, hierarchical
- Interface only
Q4. What is method overriding?
- Method overloading
- Subclass provides specific implementation
- Interface implementation
- Abstract method
Q5. What is super keyword?
- Refers to child class
- Refers to parent class members
- Static keyword
- None
Q6. Can private members be inherited?
- Yes
- No
- Sometimes
- Depends on access modifier
Q7. What is polymorphism?
- Static typing
- Objects can take many forms
- Single inheritance
- Multiple inheritance
Q8. Can constructors be inherited?
- Yes
- No
- Sometimes
- Always
Q9. Difference between extends and implements?
- Extends for interfaces
- Implements for classes
- Extends for classes, implements for interfaces
- Both same
Q10. How does inheritance promote code reuse?
- Doesn't promote
- Sharing properties and methods
- Only for abstract classes
- Only for interfaces
๐ก Bonus Insight
Inheritance simplifies complex systems by enabling hierarchical relationships and code reuse, essential for scalable Java applications.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!