1. What is Inheritance and Why is it Needed?
Inheritance is a key concept in Object-Oriented Programming (OOP) that allows a class
(child/subclass) to acquire the properties and behaviors (fields and methods) of another class
(parent/superclass).
Need for Inheritance:
• Code Reusability: Avoids redundancy by allowing subclasses to reuse parent class code.
• Extensibility: New functionalities can be added to subclasses without modifying the parent
class.
• Polymorphism: Enables dynamic method dispatch (method overriding).
• Improved Maintainability: Easier to manage and update the codebase.
2. Types of Inheritance and Examples
(a) Single Inheritance
A subclass inherits from a single superclass.
class Animal {
void eat() { System.out.println("This animal eats food."); }
class Dog extends Animal {
void bark() { System.out.println("Dog barks."); }
public class TestSingleInheritance {
public static void main(String args[]) {
Dog d = new Dog();
d.eat(); // Inherited method
d.bark();
Output:- This animal eats food.
Dog barks.
(b) Multilevel Inheritance
A subclass inherits from another subclass.
class Animal {
void eat() { System.out.println("This animal eats food."); }
class Dog extends Animal {
void bark() { System.out.println("Dog barks."); }
class Puppy extends Dog {
void weep() { System.out.println("Puppy weeps."); }
public class TestMultilevelInheritance {
public static void main(String args[]) {
Puppy p = new Puppy();
p.eat(); // Inherited from Animal
p.bark(); // Inherited from Dog
p.weep();
class Animal {
void eat() { System.out.println("This animal eats food."); }
class Dog extends Animal {
void bark() { System.out.println("Dog barks."); }
class Puppy extends Dog {
void weep() { System.out.println("Puppy weeps."); }
public class TestMultilevelInheritance {
public static void main(String args[]) {
Puppy p = new Puppy();
p.eat(); // Inherited from Animal
p.bark(); // Inherited from Dog
p.weep();
OUTPUT:- This animal eats food.
Dog barks.
Puppy weeps.
(c) Hierarchical Inheritance
Multiple subclasses inherit from a single superclass.
class Animal {
void eat() { System.out.println("This animal eats food."); }
class Dog extends Animal {
void bark() { System.out.println("Dog barks."); }
class Cat extends Animal {
void meow() { System.out.println("Cat meows."); }
public class TestHierarchicalInheritance {
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
d.bark();
Cat c = new Cat();
c.eat();
c.meow();
OUTPUT:- This animal eats food.
Dog barks.
This animal eats food.
Cat meows.
(d) Hybrid Inheritance (Achieved through Interfaces in Java)
Since Java does not support multiple inheritance through classes, Hybrid inheritance is implemented
using interfaces.
3. Why Multiple Inheritance through Classes is Not Supported in Java?
Multiple inheritance occurs when a class inherits from more than one class. Java does not support
multiple inheritance using classes to avoid the Diamond Problem.
Diamond Problem:
If two parent classes have the same method and the child class inherits from both, there is ambiguity
about which method to execute.
class A {
void show() { System.out.println("Class A"); }
class B {
void show() { System.out.println("Class B"); }
class C extends A, B { // ERROR: Java does not support multiple inheritance
To resolve this, Java allows multiple inheritance using interfaces
interface A {
void show();
interface B {
void display();
class C implements A, B {
public void show() { System.out.println("Class A show method"); }
public void display() { System.out.println("Class B display method"); }
public class TestMultipleInheritance {
public static void main(String args[]) {
C obj = new C();
obj.show();
obj.display();
OUTPUT:- Class A show method
Class B display method
4. Method Overriding with Example
Method overriding occurs when a subclass provides a specific implementation of a method already
defined in its superclass.
Rules for Method Overriding:
1. The method in the subclass must have the same signature (name and parameters) as in the
superclass.
2. The method in the subclass should have the same return type (or a subclass of the return
type in Java 5+).
3. The access modifier of the overriding method cannot be more restrictive than the superclass
method.
4. The method cannot be static, private, or final in the superclass.
class Animal {
void makeSound() { System.out.println("Animal makes a sound"); }
class Dog extends Animal {
@Override
void makeSound() { System.out.println("Dog barks"); }
public class TestMethodOverriding {
public static void main(String args[]) {
Animal a = new Dog(); // Upcasting
a.makeSound(); // Calls overridden method in Dog
4. Method Overriding with Example
Method overriding occurs when a subclass provides a specific implementation of a method already
defined in its superclass.
Rules for Method Overriding:
1. The method in the subclass must have the same signature (name and parameters) as in the
superclass.
2. The method in the subclass should have the same return type (or a subclass of the return
type in Java 5+).
3. The access modifier of the overriding method cannot be more restrictive than the superclass
method.
4. The method cannot be static, private, or final in the superclass.
Example:-
class Animal {
void makeSound() { System.out.println("Animal makes a sound"); }
class Dog extends Animal {
@Override
void makeSound() { System.out.println("Dog barks"); }
public class TestMethodOverriding {
public static void main(String args[]) {
Animal a = new Dog(); // Upcasting
a.makeSound(); // Calls overridden method in Dog
} OUTPUT:- Dog barks
Compile-Time Polymorphism Example (Method Overloading)
class MathOperations {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
public class TestOverloading {
public static void main(String args[]) {
MathOperations obj = new MathOperations();
System.out.println(obj.add(5, 10)); // Calls int version
System.out.println(obj.add(5.5, 2.2)); // Calls double version
OUTPUT:- 15
7.7
Run-Time Polymorphism Example (Method Overriding)
class Parent {
void show() { System.out.println("Parent class method"); }
}
class Child extends Parent {
@Override
void show() { System.out.println("Child class method"); }
public class TestPolymorphism {
public static void main(String args[]) {
Parent obj = new Child();
obj.show(); // Calls overridden method in Child
OUTPUT:- Child class method