0% found this document useful (0 votes)
9 views7 pages

Java Inheritance Types Explained

The document explains different types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance, with definitions and example programs for each type. It highlights the advantages of inheritance such as code reusability and supports method overriding and polymorphism. A summary table is also provided to compare the types of inheritance with their descriptions and example classes used.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Java Inheritance Types Explained

The document explains different types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance, with definitions and example programs for each type. It highlights the advantages of inheritance such as code reusability and supports method overriding and polymorphism. A summary table is also provided to compare the types of inheritance with their descriptions and example classes used.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like