Important Interview Questions and Answers on Java Inheritance
Prepare for your next Java interview with these important interview questions and answers on Java inheritance, covering key concepts, examples, and best practices.
1. What is inheritance in Java?
Answer:
Inheritance is a mechanism in Java where one class (child or subclass) acquires the properties and behaviors (methods and fields) of another class (parent or superclass). It helps in code reusability and method overriding. In Java, inheritance is implemented using the extends keyword.
Example:
class Parent {
void show() {
System.out.println(“This is a parent class”);
}
}
class Child extends Parent {
void display() {
System.out.println(“This is a child class”);
}
}
2. What are the types of inheritance in Java?
Answer:
Java supports the following types of inheritance:
- Single Inheritance – A subclass inherits from a single-parent class.
- Multilevel Inheritance – A subclass inherits from a parent class, which itself is a subclass of another class.
- Hierarchical Inheritance – Multiple subclasses inherit from the same parent class.
- Multiple Inheritance (via Interface) – Java does not support multiple inheritance with classes but allows it through interfaces.
Java does not support multiple inheritance with classes to avoid ambiguity problems (Diamond Problem).
3. Why does Java not support multiple inheritance with classes?
Answer:
Java does not support multiple inheritance with classes because it can lead to the Diamond Problem. If two parent classes have methods with the same name, the compiler cannot determine which method the child class should inherit.
Example of Diamond Problem (which is avoided in Java):
class A {
void show() {
System.out.println(“Class A”);
}
}
class B {
void show() {
System.out.println(“Class B”);
}
}
// This would cause ambiguity if Java allowed multiple inheritance
class C extends A, B { // Not allowed in Java
}
Instead, Java allows multiple inheritance using interfaces.
Intermediate Questions:
4. How does Java achieve multiple inheritance?
Answer:
Java achieves multiple inheritance using interfaces.
Example:
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println(“Method A”);
}
public void methodB() {
System.out.println(“Method B”);
}
}
public class Test {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
5. What is method overriding in inheritance?
Answer:
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its parent class. The overridden method must have the same name, return type, and parameters as the method in the superclass.
Example:
class Parent {
void display() {
System.out.println(“Display from Parent”);
}
}
class Child extends Parent {
@Override
void display() {
System.out.println(“Display from Child”);
}
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.display(); // Calls Child’s method due to runtime polymorphism
}
}
6. Can a subclass inherit private members of the parent class?
Answer:
No, private members of the parent class are not accessible in the child class. However, the subclass inherits them in an indirect way and can access them using getter and setter methods.
Example:
class Parent {
private int data = 10;
public int getData() {
return data;
}
}
class Child extends Parent {
void show() {
System.out.println(“Data: ” + getData());
}
}
Advanced Questions:
7. What is the super keyword in Java?
Answer:
The super keyword is used to refer to the immediate parent class. It can be used for:
- Calling parent class constructors
- Accessing parent class methods
- Accessing parent class fields
Example:
class Parent {
Parent() {
System.out.println(“Parent Constructor”);
}
}
class Child extends Parent {
Child() {
super(); // Calls the Parent class constructor
System.out.println(“Child Constructor”);
}
}
8. What is constructor chaining in inheritance?
Answer:
Constructor chaining occurs when a subclass constructor implicitly or explicitly calls a superclass constructor using super(). It ensures that the parent class is initialized before the child class.
Example:
class A {
A() {
System.out.println(“A’s Constructor”);
}
}
class B extends A {
B() {
super(); // Calls A’s constructor
System.out.println(“B’s Constructor”);
}
}
public class Test {
public static void main(String[] args) {
B obj = new B();
}
}
Output:
A’s Constructor
B’s Constructor
9. What is the difference between method overloading and method overriding?
Feature | Method Overloading | Method Overriding |
---|---|---|
Definition | Same method name, different parameters within the same class | Same method name and parameters in parent and child classes |
Inheritance | Not required | Required |
Return Type | Can be different | Must be the same or covariant |
Access Modifier | Can be anything | Cannot reduce the visibility of the method |
Static Methods | Can be overloaded | Cannot be overridden |
10. Can you prevent inheritance in Java?
Answer:
Yes, inheritance can be prevented using the final keyword.
Example:
final class Parent {
}
// This will cause an error
class Child extends Parent { // Compilation error: cannot inherit from final class
}
Similarly, you can prevent method overriding by marking a method as final:
class Parent {
final void show() {
System.out.println(“This method cannot be overridden”);
}
}
class Child extends Parent {
// This will cause an error
// void show() { System.out.println(“Trying to override”); }
}
Do visit our channel to learn more: Click Here
Author:-
Vaishali Sonawane
Call the Trainer and Book your free demo Class For Java Call now!!!