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

Relationship Types in Java

The document outlines four relationship types in Java: IS-A (inheritance), Association (loose relationship), Aggregation (weak ownership), and Composition (strong ownership). It provides examples and Java programs for each relationship type, demonstrating how classes interact and depend on one another. The document concludes with key points and outputs for each program, illustrating the concepts effectively.

Uploaded by

Nisha Rathi
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)
13 views5 pages

Relationship Types in Java

The document outlines four relationship types in Java: IS-A (inheritance), Association (loose relationship), Aggregation (weak ownership), and Composition (strong ownership). It provides examples and Java programs for each relationship type, demonstrating how classes interact and depend on one another. The document concludes with key points and outputs for each program, illustrating the concepts effectively.

Uploaded by

Nisha Rathi
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

Relationship Types in Java

1. IS-A (Inheritance)

Represents an “is-a” relationship where a subclass inherits properties and behaviors from a superclass.

2. Association (Normal Relationship)

A loosely coupled relationship where two independent classes communicate or interact with each other.

3. Aggregation (Has-A Relationship)

A special form of association where one class contains or owns another, but the contained object can
exist independently.

4. Composition (Strong Ownership)

A strong form of aggregation where the lifetime of the contained object depends entirely on the owner
class.
Aim:

To write a Java program that demonstrates the IS-A relationship using inheritance.

Theory:

The IS-A relationship represents inheritance in Java.

 When one class extends another, it inherits its properties and methods.
 Example: Dog IS-A Animal.

Key Points:

 Uses extends keyword


 Child class inherits parent class features
 Represents specialization

Program:

class Animal
{
void eat() { [Link]("Animal eats food"); }
}

class Dog extends Animal


{ // Dog IS-A Animal
void bark() { [Link]("Dog barks"); }
}

public class ISARelationshipDemo


{
public static void main(String[] args)
{
Dog d = new Dog();
[Link](); // inherited method
[Link](); // own method
}
}

Output:

Animal eats food


Dog barks

Conclusion:

The program demonstrates the IS-A relationship where the class Dog inherits from class Animal.
Aim:

To write a Java program demonstrating Association between two independent classes.

Theory:

Association represents a loose relationship where two classes are connected but can exist
independently.

Key Points:

 “Uses-A” relationship
 No ownership
 Both classes have independent lifecycles

Program:

class Teacher
{
String name;
Teacher(String name) { [Link] = name; }
}

class Student
{
String name;
Student(String name) { [Link] = name; }
void worksWith(Teacher t) { [Link]([Link] + " works with " + [Link]); }
}

public class AssociationDemo


{
public static void main(String[] args)
{
Teacher t = new Teacher("Mrs. Sharma");
Student s = new Student("Aman");
[Link](t);
}
}

Output: Aman works with Mrs. Sharma

Conclusion:

The program displays Association between Student and Teacher—both can exist independently.
Aim:

To write a Java program demonstrating Aggregation.

Theory:

Aggregation is a weak form of ownership, where one class contains another class object, but the
contained object can exist independently.

Key Points:

 Weak “Has-A”
 Contained object can survive even after container object is destroyed
 Example: Department has Professors

Program:

class Address
{
String city, state;
Address(String city, String state) { [Link] = city; [Link] = state; }
}

class Employee
{
String name;
Address address; // Aggregation

Employee(String name, Address address) { [Link] = name; [Link] = address; }

void display() { [Link](name + " lives in " + [Link] + ", " + [Link]); }
}

public class AggregationDemo


{
public static void main(String[] args)
{
Address a = new Address("Pune", "Maharashtra");
Employee e = new Employee("Rohan", a);
[Link]();
}
}

Output:

Rohan lives in Pune, Maharashtra

Conclusion:

The program demonstrates Aggregation since Employee uses Address, but Address exists independently.
Aim:

To write a Java program demonstrating Composition in Java.

Theory:

Composition is a strong form of Has-A relationship where the lifetime of the contained object is
completely dependent on the container class.

Key Points:

 Strong ownership
 Part cannot exist without the whole
 Example: House HAS Rooms

Program:

class Engine
{
void start() { [Link]("Engine started"); }
}

class Car {
private Engine engine = new Engine(); // Composition

void startCar() { [Link](); [Link]("Car is running"); }


}

public class CompositionDemo


{
public static void main(String[] args)
{
Car c = new Car();
[Link]();
}
}

Output:

Engine started

Car is running

Conclusion:

Composition is shown because Engine is created inside Car and cannot exist independently of it.

You might also like