Static Nested Inner Classes
Perfect, Saran 🌸
Here’s the detailed explanation and separate example programs for each type of
inheritance in Java — neat and suitable for 13-mark answers.
🧩 INHERITANCE IN JAVA
Definition:
Inheritance is a mechanism in which one class (child class) acquires the properties
and behaviors (methods and variables) of another class (parent class) using the
extends keyword.
1. Single Inheritance
Definition:
In single inheritance, one class inherits from another class — a one-to-one
relationship.
Diagram:
nginx
Parent
↓
Child
Program:
java
// Single Inheritance Example
class Animal {
void eat() {
[Link]("Animals can eat");
}
}
1/7
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // inherited from Animal
[Link](); // own method
}
}
Output:
nginx
Animals can eat
Dog barks
2. Multilevel Inheritance
Definition:
When a class is derived from another derived class. It forms a chain of inheritance.
Diagram:
nginx
Grandparent
↓
Parent
↓
Child
Program:
java
2/7
// Multilevel Inheritance Example
class Animal {
void eat() {
[Link]("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
[Link]("Puppy weeps");
}
}
public class MultilevelInheritance {
public static void main(String[] args) {
Puppy p = new Puppy();
[Link](); // from Animal
[Link](); // from Dog
[Link](); // from Puppy
}
}
Output:
nginx
Animals eat food
Dog barks
Puppy weeps
3. Hierarchical Inheritance
Definition:
In hierarchical inheritance, multiple classes inherit from a single parent class.
3/7
Diagram:
markdown
Animal
/ \
Dog Cat
Program:
java
// Hierarchical Inheritance Example
class Animal {
void eat() {
[Link]("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
void meow() {
[Link]("Cat meows");
}
}
public class HierarchicalInheritance {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
Cat c = new Cat();
[Link]();
[Link]();
}
}
4/7
Output:
powershell
Animals eat food
Dog barks
Animals eat food
Cat meows
4. Hybrid Inheritance (Using Interface)
Definition:
Hybrid inheritance is a combination of two or more types of inheritance.
Since Java does not support multiple inheritance with classes, it is achieved using
interfaces.
Diagram:
vbnet
Interface
↑
Class A Class B
\ /
\ /
Derived Class
Program:
java
// Hybrid Inheritance Example using Interface
interface Animal {
void eat();
}
class Dog implements Animal {
public void eat() {
[Link]("Dog eats food");
}
}
5/7
interface Pet {
void play();
}
class Puppy extends Dog implements Pet {
public void play() {
[Link]("Puppy loves to play");
}
}
public class HybridInheritance {
public static void main(String[] args) {
Puppy p = new Puppy();
[Link]();
[Link]();
}
}
Output:
css
Dog eats food
Puppy loves to play
Summary Table
Type Description Example Class Used
Single One class inherits Dog extends Animal
another
Multilevel Derived class from Puppy extends Dog
another derived
class
Hierarchical Multiple classes Dog and Cat extend
inherit same Animal
parent
6/7
Type Description Example Class Used
Hybrid Combination using Puppy extends Dog
interfaces implements Pet
Advantages of Inheritance
1. Promotes code reusability.
2. Enhances readability and maintainability.
3. Supports method overriding and polymorphism.
4. Establishes logical hierarchy between classes.
Would you like me to combine all these 4 examples into one formatted PDF (ready
for notes or submission)?
7/7