0% found this document useful (0 votes)
13 views22 pages

"Is-A" Relationship in Java (Inheritance)

The document explains the 'Is-A' relationship in Java, which represents inheritance where a child class derives properties from a parent class, supporting code reusability and polymorphism. It outlines types of inheritance, such as single, multilevel, hierarchical, and multiple inheritance through interfaces, along with real-life examples like animals and vehicles. Additionally, it discusses the 'Has-A' relationship, representing composition and aggregation, with examples like cars and engines, emphasizing the differences between strong and weak associations.

Uploaded by

dahiyaneha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

"Is-A" Relationship in Java (Inheritance)

The document explains the 'Is-A' relationship in Java, which represents inheritance where a child class derives properties from a parent class, supporting code reusability and polymorphism. It outlines types of inheritance, such as single, multilevel, hierarchical, and multiple inheritance through interfaces, along with real-life examples like animals and vehicles. Additionally, it discusses the 'Has-A' relationship, representing composition and aggregation, with examples like cars and engines, emphasizing the differences between strong and weak associations.

Uploaded by

dahiyaneha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

"Is-A" Relationship in Java

(Inheritance)
"Is-A" Relationship in Java
(Inheritance)

The "Is-A" relationship in Java represents inheritance, where one class


derives properties and behaviors from another class. This means the child
class is a specialized version of the parent class.
Key Points About Inheritance:
1.Parent-Child Relationship: The child class inherits attributes and
behaviors (methods) from the parent class.
2.Code Reusability: Instead of rewriting the same code in multiple
classes, we define common functionalities in the parent class and
reuse them in child classes.
3.Polymorphism Support: Inheritance enables method overriding,
which allows a child class to provide a specific implementation of a
method already defined in the parent class.
4.Extending Functionality: A child class can have additional attributes
and methods apart from the inherited ones.
Types of Inheritance in Java:
1.Single Inheritance – A child class inherits from a single parent class.
2.Multilevel Inheritance – A child class inherits from a parent class,
which itself is a child of another class (forming a chain).
3.Hierarchical Inheritance – A single parent class has multiple child
classes.
4.Multiple Inheritance (Through Interfaces) – Java does not support
multiple inheritance with classes, but it can be achieved using
interfaces.
Real-Life Examples of Inheritance in Java
Inheritance in Java follows an "is-a" relationship, meaning a child class is a specialized
version of a parent class. Here are some real-world examples:

1. Animal Kingdom (Hierarchical Inheritance)

•Parent Class: Animal (Has common properties like breathing, eating)


•Child Classes:
•Mammal (Has properties like warm-blooded)
•Bird (Has properties like feathers, can fly)
•Fish (Has properties like can swim, gills)

Example Relationship:
•A dog is a mammal → A mammal is an animal (Multilevel Inheritance).
•A sparrow is a bird → A bird is an animal (Hierarchical Inheritance).
Real-Life Examples of Inheritance in Java
Inheritance in Java follows an "is-a" relationship, meaning a child class is a specialized
version of a parent class. Here are some real-world examples:

2. Vehicle Classification (Single Inheritance)


•Parent Class: Vehicle (Has properties like speed, fuel type)
•Child Class: Car (Inherits speed and fuel type but has additional properties like airbags, AC)

Example Relationship:
•A car is a vehicle.
•A truck is a vehicle.
Real-Life Examples of Inheritance in Java
Inheritance in Java follows an "is-a" relationship, meaning a child class is a specialized
version of a parent class. Here are some real-world examples:

3. Employee Management System (Multilevel Inheritance)


•Parent Class: Employee (Has properties like name, ID, salary)
•Child Class: Manager (Inherits name, ID, and salary but has additional properties like team
size)
•Grandchild Class: ProjectManager (Inherits everything from Manager but adds project
name)

Example Relationship:
•A Project Manager is a Manager.
•A Manager is an Employee.
Real-Life Examples of Inheritance in Java
Inheritance in Java follows an "is-a" relationship, meaning a child class is a specialized
version of a parent class. Here are some real-world examples:

4. Educational Institution (Hierarchical Inheritance)

•Parent Class: Person (Has properties like name, age)


•Child Classes:
•Student (Has properties like roll number, grades)
•Teacher (Has properties like subject specialization, salary)
•Staff (Has properties like department, working hours)

Example Relationship:
•A student is a person.
•A teacher is a person.
Real-Life Examples of Inheritance in Java
Inheritance in Java follows an "is-a" relationship, meaning a child class is a specialized
version of a parent class. Here are some real-world examples:

5. Banking System (Multiple Inheritance using Interfaces)

•Interface 1: LoanProvider (Has method provideLoan())


•Interface 2: AccountHolder (Has method deposit(), withdraw())
•Child Class: Bank (Implements both interfaces, meaning it provides loans and
allows deposits/withdrawals)

Example Relationship:
•A bank is both a loan provider and an account holder.
Has-A relation
Aggregation & Composition
• The "Has-A" relationship in Java represents composition or
aggregation, where one class contains an instance of another class as
a field.
• Instead of inheriting properties (as in "Is-A" relationship through
inheritance), a class uses another class as part of its functionality.
Key Points About "Has-A" Relationship:

1.Achieved Using Instance Variables – A class holds an object reference of


another class.
2.Promotes Code Reusability – Instead of duplicating properties, a class can
use another class’s methods and attributes.
3.Types:
•Composition (Strong Association) – If the contained object cannot exist
independently without the container class (e.g., Car and Engine).
•Aggregation (Weak Association) – If the contained object can exist
independently of the container class (e.g., University and Professor).
When to Use Composition vs. Aggregation?

✅ Use Composition when:


•The contained object is a part of the container and cannot exist separately.
•Example: A Car has an Engine, a Human has a Heart.

✅ Use Aggregation when:


•The contained object is associated with the container but can exist
independently.
•Example: A Library has Books, a Company has Employees.
Real-Life Examples of "Has-A"
Relationship:
1. Car and Engine (Composition - Strong Association)
•A Car has an Engine.
•The Engine cannot exist separately without the Car.
•If the Car is destroyed, the Engine is also removed.
📌 Example:
•A Car object contains an Engine object.
•The Engine is tightly bound to the Car and cannot function separately.
Real-Life Examples of "Has-A"
Relationship:
2. University and Professor (Aggregation - Weak Association)
•A University has Professors.
•A Professor can exist outside the University (can work in multiple universities).
•If the University is shut down, Professors can still work elsewhere.
📌 Example:
•The University class has a list of Professor objects.
•Each Professor can be associated with multiple universities.
Real-Life Examples of "Has-A"
Relationship:
3. Human and Heart (Composition - Strong Association)
•A Human has a Heart.
•The Heart cannot exist separately outside the Human.
•If the Human dies, the Heart stops functioning.
📌 Example:
•A Human class contains a Heart object.
•The Heart is part of the Human and cannot function independently.
Real-Life Examples of "Has-A"
Relationship:
4. Library and Books (Aggregation - Weak Association)
•A Library has Books.
•Books can exist separately outside the Library (can be sold, borrowed, or placed in
another Library).
📌 Example:
•The Library class contains a collection of Book objects.
•The Book class is independent and can be used elsewhere.
Real-Life Examples of "Has-A"
Relationship:
5. House and Rooms (Composition - Strong Association)
•A House has Rooms.
•The Rooms cannot exist separately without the House.
•If the House is demolished, the Rooms also cease to exist.
📌 Example:
•A House class contains multiple Room objects.
•Each Room belongs to a specific House.
Real-Life Examples of "Has-A"
Relationship:
6. Company and Employees (Aggregation - Weak Association)

•A Company has Employees.


•Employees can exist separately (they can work for different companies).
•If the Company shuts down, Employees can still find jobs elsewhere.
📌 Example:
•A Company class has a list of Employee objects.
•Employees are independent and not tightly coupled with the Company.
Key Differences: Composition vs. Aggregation
Difference Between "Is-A" and "Has-A" Relationship

You might also like