Inheritance lets one class get features from another. But it has limits to keep code clear and safe.
Inheritance limitations in Java
Start learning this pattern below
Jump into concepts and practice - no test required
class Parent { void show() { System.out.println("Parent class method"); } } class Child extends Parent { // Child inherits show() method }
Java allows only one direct parent class (single inheritance).
Some classes or methods can be marked final to prevent inheritance or overriding.
final class cannot be inherited.final class FinalClass { void display() { System.out.println("Can't inherit this class"); } } // This will cause an error: // class Child extends FinalClass {}
final method cannot be overridden by child classes.class Parent { final void show() { System.out.println("Can't override this method"); } } class Child extends Parent { // Trying to override show() here will cause an error }
class A {} class B {} // Java does not allow: // class C extends A, B {}
This program shows a normal inheritance from Parent to Child. It also shows that a final class like FinalParent cannot be inherited (commented out to avoid error).
class Parent { void greet() { System.out.println("Hello from Parent"); } } final class FinalParent { void greet() { System.out.println("Hello from FinalParent"); } } class Child extends Parent { // Inherits greet() } // Uncommenting below will cause compile error // class Child2 extends FinalParent {} public class Main { public static void main(String[] args) { Child c = new Child(); c.greet(); } }
Java uses final keyword to stop inheritance or method overriding.
Multiple inheritance of classes is not allowed to avoid confusion and errors.
Use interfaces if you need to inherit from multiple types.
Java allows only single inheritance of classes.
final classes and methods cannot be inherited or overridden.
Inheritance limits help keep code simple and avoid mistakes.
Practice
Which of the following is not allowed in Java inheritance?
- Extending multiple classes
- Extending a
finalclass - Overriding a
finalmethod - All of the above
Solution
Step 1: Understand Java inheritance rules
Java supports only single inheritance of classes, so extending multiple classes is not allowed.Step 2: Check restrictions on final classes and methods
Classes declared as final cannot be extended, and final methods cannot be overridden.Final Answer:
All of the above -> Option DQuick Check:
Java disallows multiple inheritance, final class extension, and final method overriding [OK]
- Thinking Java supports multiple class inheritance
- Trying to override final methods
- Assuming final classes can be extended
Which of the following class declarations is correct in Java?
public class Animal {}
public class Dog extends Animal {}
public class Cat extends Dog, Animal {}Solution
Step 1: Check single inheritance rule
Java allows a class to extend only one class. Dog extends Animal correctly.Step 2: Analyze Cat class declaration
Cat tries to extend Dog and Animal simultaneously, which is invalid syntax in Java.Final Answer:
Only Dog class declaration is correct -> Option BQuick Check:
Single inheritance means one parent only [OK]
- Trying to extend multiple classes in one declaration
- Confusing interfaces with classes for multiple inheritance
- Assuming all class declarations are valid
What will be the output of the following Java code?
final class Vehicle {
void start() { System.out.println("Vehicle started"); }
}
class Car extends Vehicle {
void start() { System.out.println("Car started"); }
}
public class Test {
public static void main(String[] args) {
Car c = new Car();
c.start();
}
}Solution
Step 1: Identify final class usage
The class Vehicle is declared final, so it cannot be extended by any class including Car.Step 2: Check inheritance and compilation
Since Car tries to extend final Vehicle, the compiler will throw an error.Final Answer:
Compilation error -> Option AQuick Check:
final class cannot be subclassed [OK]
- Assuming final class can be extended
- Expecting runtime error instead of compile error
- Thinking method overriding causes error here
Find the error in the following code snippet:
class Parent {
final void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
void show() {
System.out.println("Child show");
}
}Solution
Step 1: Understand final method behavior
Methods declared final in a parent class cannot be overridden in child classes.Step 2: Analyze Child class method
Child class tries to override final method show(), which causes a compile-time error.Final Answer:
Child class cannot override final method show() -> Option CQuick Check:
final methods block overriding [OK]
- Thinking final methods can be overridden
- Assuming no error in overriding final methods
- Confusing final methods with abstract methods
You want to prevent any class from extending your class SecureData, but still allow other classes to use its methods. Which is the best way to do this?
Solution
Step 1: Understand the effect of final class
Declaring a class as final prevents any other class from extending it, but allows normal usage of its methods.Step 2: Evaluate other options
Making methods final prevents overriding but not extending; abstract class requires subclassing; private methods are inaccessible outside the class.Final Answer:
Declare the class SecureData as final -> Option AQuick Check:
final class blocks inheritance but allows usage [OK]
- Confusing final methods with final classes
- Using abstract class which requires subclassing
- Making methods private, blocking access
