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!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup