Inheritance in Java: Building Hierarchical and Reusable Code
Inheritance is the cornerstone of reusability in Java. It allows a new class to absorb the fields and methods of an existing class, facilitating the "Is-A" relationship. For your page /Pages/java-inheritance-questions.html, we have developed a deep-dive technical guide that covers everything from basic syntax to complex memory allocation and interview-level edge cases.
Core Concept: Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an integral part of OOPs (Object Oriented Programming System).
1. The Syntax and Terminology
To implement inheritance, Java uses the extends keyword. This keyword signifies that you are creating a subclass that derives its functionality from a superclass.
- Super Class (Parent): The class whose features are inherited.
- Sub Class (Child): The class that inherits the other class. It can also add its own specific features.
- Reusability: Facilitates you to reuse the fields and methods of the existing class.
Basic Code Example
class Employee { float salary = 40000; } class Programmer extends Employee { int bonus = 10000; public static void main(String args[]) { Programmer p = new Programmer(); System.out.println("Salary is: " + p.salary); System.out.println("Bonus is: " + p.bonus); } }
2. Types of Inheritance in Java
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. In java programming, multiple and hybrid inheritance is supported through interface only.
| Type | Description | Visual Structure |
|---|---|---|
| Single Inheritance | One child class inherits from one parent class. | Class A → Class B |
| Multilevel Inheritance | A child class inherits from a parent, which is also a child of another parent. | Class A → Class B → Class C |
| Hierarchical Inheritance | Multiple child classes inherit from a single parent class. | Class A → Class B & Class C |
Why Multiple Inheritance is not supported in Java?
To reduce complexity and simplify the language, multiple inheritance is not supported in java. Java uses Interfaces to achieve similar functionality safely.
3. The Role of 'super' and Constructors
When you create an instance of a subclass, the parent class constructor is called first. This is known as Constructor Chaining. The super keyword is used to refer to the immediate parent class object.
- It can be used to refer to immediate parent class instance variable.
- It can be used to invoke immediate parent class method.
super()can be used to invoke immediate parent class constructor.
4. Comprehensive Interview Questions & Detailed Explanations
Q1. Does a subclass inherit the private members of its superclass?
No, a subclass does not inherit the private members of its parent class.
Q2. What is the difference between IS-A and HAS-A relationship?
IS-A relationship: Achieved through Inheritance (Car IS-A Vehicle).
HAS-A relationship: Achieved through Composition (Car HAS-A Engine).
Q3. Can we override a static method?
No, you cannot override a static method. Defining a static method with the same signature in the child class is called Method Hiding.
5. Advanced Theory: Upcasting and Downcasting
Upcasting: Casting a subtype to a supertype automatically. Parent p = new Child();
Downcasting: Casting a supertype to a subtype manually. Child c = (Child)p;
6. Conclusion
Inheritance is a powerful tool that, when used correctly, can significantly reduce code redundancy and improve structure. Mastering inheritance is the gateway to understanding complex Java frameworks.
Continue your journey with our next module on Polymorphism at InterviewHub.