Department of Information Technology
Experiment No: 6 Batch: S4 Date: 3/9/2025
Name: Aaditya Mishra Roll No: A-368
Aim: To study and implement inheritance in java.
Problem Statement:
An educational institute wishes to maintain a data of the employee. The databox
is divided into no. of classes whose hierarchical relationship are shown in
[Link] figure also shown minimum information required for each class.
Theory:
What is Inheritance in Java?
Inheritance in Java is a fundamental feature of object-oriented programming that
allows a class (subclass or derived class) to inherit properties and behaviors
(methods and fields) from another class (superclass or base class). This enables
code reuse and promotes a hierarchical structure among classes, facilitating
better organization and maintenance of code. This mechanism supports code
reusability and enhances the structure of programs through hierarchical
relationships between classes.
Types of Inheritance in Java:
1) Single Inheritance:
Single inheritance is a type of inheritance where a subclass inherits the
properties and behaviors from a single superclass.
Syntax:classSuperclass{}
classSubclassextendSuperclass{}
Department of Information Technology
2) Multilevel Inheritance:
Multilevel inheritance occurs when a class is derived from a class which is also
derived from another class.
Syntax:classGrandparent}
classParentextendsGrandparent{}
class Child extends Parent {}
3) Hierarchical Inheritance
Hierarchical inheritance means that multiple classes inherit from a single parent
class.
Syntax:classParentClass{}
classChildClass1extendsParentClass{}
classChildClass2extendsParentClass{}
4) Multiple Inheritance(Using Interfaces):
Java does not support multiple inheritance with classes to avoid ambiguity.
However, it is supported using interfaces.
5) Hybrid Inheritance in Java (Using Interfaces):
Hybrid inheritance is a combination of two or more types of inheritance. Java
supports hybrid inheritance through interfaces.
The keyword used for inheritance in Java is extends.
This keyword is used in a class declaration to indicate that a new class (subclass)
will inherit properties and methods from an existing class (superclass).
class Superclass {
// Superclass members
}
class Subclass extends Superclass { // Subclass members, including inherited
members from Superclass
}
Department of Information Technology
Problem Statement:
An educational institute wishes to maintain a data of the employee. The databox
is divided into no. of classes whose hierarchical relationship are shown in
[Link] figure also shown minimum information required for each class.
Source Code:
Department of Information Technology
Output:
Department of Information Technology
Conclsion:
Inheritance in Java is a core object-oriented programming concept that fosters
code reusability, promotes hierarchical relationships between classes, and
facilitates polymorphism. It allows a subclass to inherit properties and behaviors
(methods and fields) from a superclass, establishing an "IS-A" relationship.
While Java supports single, multilevel, and hierarchical inheritance directly using
the extends keyword, it intentionally avoids direct multiple inheritance to prevent
ambiguity issues that can arise when a class inherits from multiple parent classes
with conflicting members. Instead, Java achieves the benefits of multiple
inheritance through interfaces, which define contracts that classes can implement.
Ultimately, mastering inheritance is crucial for building well-structured,
maintainable, and extensible Java applications, enabling efficient code
organization and promoting a clear understanding of class relationships within a
system.
Department of Information Technology