0% found this document useful (0 votes)
153 views16 pages

Relationships in OOPs

The document discusses relationships in object-oriented programming including inheritance, association, aggregation, composition, and dependency. Inheritance represents an 'is-a' relationship where a subclass extends a superclass. Association represents a 'has-a' relationship and can be weak like aggregation where objects can exist independently, or strong like composition where the lifetimes of associated objects are tied together.

Uploaded by

seantanjunhao11
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)
153 views16 pages

Relationships in OOPs

The document discusses relationships in object-oriented programming including inheritance, association, aggregation, composition, and dependency. Inheritance represents an 'is-a' relationship where a subclass extends a superclass. Association represents a 'has-a' relationship and can be weak like aggregation where objects can exist independently, or strong like composition where the lifetimes of associated objects are tied together.

Uploaded by

seantanjunhao11
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
You are on page 1/ 16

SIT Internal

RELATIONSHIPS
IN OOPS
SIT Internal

• Relationships in OOPs
LEARNING • “IS-A”
OUTCOM ES • “HAS-A”
• Dependency
SIT Internal

WHY NEED RELATIONSHIPS?

MODEL REAL WORLD CLASS IS A BASIC UNIT MULTIPLE CLASSES SEVERAL


SCENARIOS AND OBJECTS CONNECTIONS AND
INTERACTIONS
SIT Internal

RELATIONSHIPS IN OOPS

Is-A Has-A
Inheritance Association

Aggregation Composition
SIT Internal

“IS- A” RELATIONSHIP
• Describes inheritance between classes
• Subclass being a specialized version of its superclass

“Subclass IS-A type of superclass”


SIT Internal

INHERITANCE
Person
-
-
name
age
Person
- height
- eat()
- sleep()

Faculty
- staffId
- teachingSubject
Faculty - methodOfLecture()
Student
Student
- studentId

- methodOfStudy()
SIT Internal

Faculty X IS-A Person


Student Y IS-A Person
INHERITANCE • It’s a parent-child relationship
Person
-
-
name
age
Person
- height
- eat()
- sleep()

Faculty
- staffId
- teachingSubject
Faculty - methodOfLecture()
Student
Student
- studentId

- methodOfStudy()
SIT Internal

“HAS- A” RELATIONSHIP
• Describes Association relationship between classes

• One class contains an instance of another class as a member or attribute


SIT Internal

ASSOCIATION

Weak Strong

Aggregation Composition
SIT Internal

AGGREGATION
• Whole-part relationship
• one class (the whole) contains one or more objects of another class (the part)

• They can exist independently and have their own lifetime.


• When the whole object is destroyed, the associated parts are not
necessarily destroyed with it.

Part Whole
SIT Internal

AGGREGATION: EXAMPLE

Employee Department

• Department object will own the Employee class’s objects


• A Department can contain multiple Employee objects
• But they have their own object timeline public class Employee {
• They can exist without each other private String name;
private int employeeId;
private Department department;

}
SIT Internal

COMPOSITION
• Whole-part relationship
• one class (the whole) contains one or more objects of another class (the part)
• The associated objects are exclusive to the whole object and cannot exist
independently.
• When the whole object is destroyed, its parts are also destroyed.

Part Whole
SIT Internal

COMPOSITION: EXAMPLE

Engine Car

• Car object will own the Engine class’s objects


• When the Car is created, it creates an Engine
• Lifetimes for both are the same public class Car {
• When the Car is destroyed, the Engine is also destroyed private String make;
private String model;
private Engine engine;
}
SIT Internal

DEPENDENCY
• One class depends on another
• One class uses functionality or service of the other class
• Can exist between objects while they are associated, aggregated or
composed

Dependency Dependent
Class Class
SIT Internal

public class Calculator {


private MathLibrary mathLib;

public Calculator() {
DEPENDENCY: EXAMPLE // Create an instance of the
MathLibrary to use its functions
this.mathLib = new MathLibrary();
}

Math Calculator
Library Class

• Calculator uses the functions from the "MathLibrary" to perform its mathematical
calculations.
• When the Calculator is created, it creates an instance of Math library as part of its
constructor
SIT Internal

CONCLUSION
• Different relationships in OOPs
• “is-a”: Inheritance
• “has-a”: Association
• Aggregation
• Composition
• Dependency

You might also like