0% found this document useful (0 votes)
101 views29 pages

Class Relationships: Based On Slides of Dr. Norazah Yusof

The document discusses different types of class relationships in object-oriented programming, including association, aggregation, composition, and inheritance. Association represents a general relationship between two classes and allows objects to reference each other and call methods. Aggregation models "has-a" relationships where one class is part of another, but can exist independently. Composition represents exclusive ownership where the aggregate object has sole responsibility for the composed object.

Uploaded by

Ida Ramona
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)
101 views29 pages

Class Relationships: Based On Slides of Dr. Norazah Yusof

The document discusses different types of class relationships in object-oriented programming, including association, aggregation, composition, and inheritance. Association represents a general relationship between two classes and allows objects to reference each other and call methods. Aggregation models "has-a" relationships where one class is part of another, but can exist independently. Composition represents exclusive ownership where the aggregate object has sole responsibility for the composed object.

Uploaded by

Ida Ramona
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/ 29

Class Relationships

Lecture 18
Based on Slides of Dr. Norazah Yusof

Relationships among Classes


Association

Aggregation
Composition

Inheritance

Association
Association represents a general binary relationship
that describes an activity between two classes.
Student

5..60

Take

public class Student {


/** Data fields */
private Course[]
courseList;

* Course

0..3

Teach

public class Course {


/** Data fields */
private Student[]
classList;
private Faculty faculty;

/** Constructors */
/** Methods */

/** Constructors */
/** Methods */

1
Teacher

Faculty

public class Faculty {


/** Data fields */
private Course[]
courseList;
/** Constructors */
/** Methods */
}

An association is usually represented as a data field in the


class.

Association

Example: Student register course.


register(Course)

Student

5..60

Course

The arrow is optional and specifies

navigability.
No arrow implies bidirectional navigability
4

Association

An association is usually represented as a data field in


the class.
Student

-name : String
-matrix : String
-courseList : Course[]
-numOfStudent : int
+ Student(String, String,
String)
+ getName() : String
+ getMatrix(): String
+ getNumOfStudent(): int
+ registerCourse(Course)
: void
+ printAllInfo():void

Course
-name : String
-code : String
- section : int

registerCourse(Course)

5..60

public class Student


{ /*attributes*/
private Course[] courseList;
/*methods*/}

*
+ Course(String, String,
int)
+ getName() : String

public class Course


{/*attributes*/
/*methods*/}

Association

The relationship allows objects to call methods in other


objects.
Student
Course

-name : String
-matrix : String
-courseList : Course[]
-numOfStudent : int
+ Student(String, String,
String)
+ getName() : String
+ getMatrix(): String
+ getNumOfStudent(): int
+ registerCourse(Course)
: void
+ printAllInfo():void

-name : String
-code : String
- section : int

registerCourse(Course)

5..60

public class Student


{ /*attributes*/
private Course[] courseList;
/*methods*/}

*
+ Course(String, String,
int)
+ getName() : String

public class Course


{/*attributes*/
/*methods*/}

Association

This is the same as an object sending another


object a message.
Therefore, it is implemented as object
references.
Student

-name : String
-matrix : String
-courseList : Course[]
-numOfStudent : int
+ Student(String, String,
String)
+ getName() : String
+ getMatrix(): String
+ getNumOfStudent(): int
+ registerCourse(Course)
: void
+ printAllInfo():void

Course
registerCourse(Course)

5..60

-name : String
-code : String
- section : int

*
+ Course(String, String,
int)
+ getName() : String

Association Example 1: Student register Course


1 import java.util.*;
2 public class TestAssociate{
3
public static void main(String args[]){
4
Course cs1 = new Course("OOP","SCP3103",3);
5
Course cs2 = new Course("TP1","SCJ1013",3);
6
Course cs3 = new Course("TP2","SCJ1213",3);
7
Course cs4 = new Course("KP","SCP2113",3);
8
9
Student s1 = new Student ("ALI","AC1234","2SCS");
10
s1.registerCourse(cs1);
11
s1.registerCourse(cs2);
12
s1.printAllInfo();
13
System.out.println();
14
15
Student s2 = new Student ("AHMAD","AC1122","3SCK");
16
s2.registerCourse(cs1);
17
s2.registerCourse(cs3);
18
s2.registerCourse(cs4);
19
s2.printAllInfo();
20
}
21 }

Association Example 1: Student register Course


(Student class)
1 public class Student {
2
private String name;
3
private String matrix;
4
private Course[] courseList; //showing association
5
private int numOfCourse;
6
7
public Student(String n,String m,String c){
8
name=n;
9
matrix=m;
10
courseList = new Course[10]; //showing association
11
}
12
13
public String getName() {
14
return name;
15
}
16
public String getMatrix() {
17
return matrix;
18
19
}
9

Association Example 1: Student register Course


(Student class cont.)
21 public void registerCourse(Course cs) //showing association
22 {
courseList[numOfCourse] = cs;
23
numOfCourse++;
24
}
25
26
public int getNumOfCourse() {
27
return numOfCourse;
28
}
29
public void printAllInfo() {
30
System.out.println("\nSTUDENT NAME
:"+name);
31
System.out.println("NUMBER OF SUBJECT(s) TAKEN :" +
32
numOfCourse);
33
System.out.println("LIST OF SUBJECT(s) TAKEN
:");
34
for(int i=0;i<numOfCourse;i++) {
35
Course s=(Course)courseList[i];
36
System.out.println((i+1) + ". " + s.getName());
37
}
38
}
39 }
10

Association Example 1: Student register Course


(Course class)
1 public class Course {
2
private String name;
3
private String code;
4
private int section;
5
public Course(String n,String c,int s){
6
name = n;
7
code = c;
8
section =s;
9
}
10
11
public String getName() {
12
return name;
13
}
14 }

11

Association Example 2: Lecturer teach Course

Write the classes and test the classes to show


association relationships among them.
Lecturer
-name : String
-courseList : Course[]
-numOfCourse : int

Course
-name : String
-code : String
- section : int
+ Course(String, String,
int)
+ getName() : String

public class Course


{/*attributes*/
/*methods*/}

Teach(Course)

0..3

+ Lecturer (String)
+ getName() : String
+ getNumOfCourse(): int
+ teach(Course): void
+ printAllInfo():void

public class Lecturer


{ /*attributes*/
private Course[] courseList;
/*methods*/}

12

Association Example 3: Bidirectional Relationship


Student register Course and Course add Students

Student
-name : String
-matrix : String
-courseList : Course[]
-numOfStudent : int

+ Student(String, String,
String)
+ getName() : String
+ getMatrix(): String
+ getNumOfStudent(): int
+ registerCourse(Student):
void
+ printAllInfo():void

public class Student


{ /*attributes*/
private Course[] courseList;
/*methods*/}

Course
register(Course)

5..60
0..3

add(Student)

-name : String
* -code : String
1 - section : int
- stuList: Student[]
+ Course(String, String,
int)
+ getName() : String
+ addStudent(Course):
void
+ printAllInfo():void

public class Course


{/*attributes*/
private Student[] stuList;

/*methods*/}

13

Association Example 3: Bidirectional Relationship


Student register Course and Course add Students
1 import java.util.*;
2 public class TestAssociate2 {
public static void main(String args[]){
3
Course1 cs1 = new Course1("OOP","SCP3103",3);
4
Course1 cs2 = new Course1("TP1","SCJ1013",3);
5
Course1 cs3 = new Course1("TP2","SCJ1213",3);
6
Course1 cs4 = new Course1("KP","SCP2113",3);
7
Student1 s1 = new Student1("Ali","AC0021","2SCK");
8
Student1 s2 = new Student1("Abu","AC0022","3SCK");
9
Student1 s3 = new Student1("Ben","AC0023","3SCS");
10
cs1.addStudent(s1);
11
cs1.addStudent(s2);
12
cs1.addStudent(s3);
13
cs2.addStudent(s2);
14
cs3.addStudent(s2);
15
cs1.printAllInfo();
16
cs2.printAllInfo();
17
s1.printAllInfo();
18
19
s2.printAllInfo();
20
s3.printAllInfo();
21
}
22 }

14

Association Example 3: Bidirectional Relationship


Student register Course and Course add Students
1 public class Student1 {
2
private String name;
3
private String matrix;
4
private String major;
5
private Course1[] courseList; //showing association
6
private int numOfCourse;
7
public Student1(String n,String m,String j){
8
name=n;
9
matrix=m;
10
major = j;
11
courseList = new Course1[4]; //showing association
12
}
13
public String getName() {
14
return name;
15
}
16
public String getMatrix() {
17
return matrix;
18
}
19
15

Association Example 3: Bidirectional Relationship


Student register Course and Course add Students
21 public String getMajor() {
22
return major;
23
}
24
public void register(Course1 cs) { //showing association
25
courseList[numOfCourse] = cs;
26
numOfCourse++;
27
}
28
public int getNumOfCourse() {
29
return numOfCourse;
30
}
31
public void printAllInfo() {
32
System.out.println("\nSTUDENT NAME
:"+name);
33
System.out.println("NUMBER OF SUBJECT(s)
34 TAKEN :"+numOfCourse);
35
System.out.println("LIST OF SUBJECT(s) TAKEN
:");
36
for(int i=0;i<numOfCourse;i++) {
37
Course1 s=(Course1)courseList[i];
38
System.out.println((i+1) + ". " + s.getName());
39
}
40
}
41 }

16

Association Example 3: Bidirectional Relationship


Student register Course and Course add Students
1 public class Course1 {
2
private String name;
3
private String code;
4
private int section;
5
private Student1[] stuList; //showing association
6
private int numOfStudent;
7
public Course1(String n,String c,int s){
8
name = n;
9
code = c;
10
section =s;
11
stuList = new Student1[60];
12
}
13
public String getName()
{
14
return name;
15
}
16
public void addStudent(Student1 st){
//showing association
17
stuList[numOfStudent] = st;
18
numOfStudent++;
19
st.register(this);
20
}//addStudent

17

Association Between Same Class


Association may exist between objects of the same
class. For example, a person may have a supervisor.

Person
Supervisor
1

18

Aggregation and Composition


Aggregation is a special form of association, which

represents an ownership relationship between two


classes.
Aggregation models the has-a relationship.
If an object is exclusively owned by an aggregated
object, the relationship between the object and its
aggregated object is referred to as composition.
Composition

Name

Aggregation

Person

Address
19

Representing Aggregation in Classes


An aggregation relationship is usually
represented as a data field in the aggregated
class.
public class Name {
/** Data fields */
/** Constructors */
/** Methods */
}

public class Person {


/** Data fields */
private Name name;
private Address address;

public class Address {


/** Data fields */
/** Constructors */
/** Methods */
}

/** Constructors */
/** Methods */
}

20

Inner Classes Translation


If Name or Address is used in the Person class
only, they can be declared as an inner class in
Person. For example,
public class Person {
private Name name;
private Address address;
...
class Name {
...
}
class Address {
...
}
}

21

Aggregation Example 1: FSKSM building has


Lab, LectureRoom and LecturerRoom

public class FSKSMBuilding


{

private Lab lab1;


private LectureRoom lect;
private LecturerRoom lectr;

}
22
Aggregation relationship is represented as a data field in the aggregated class.

Composition

Form of aggregation.
For a relationship: contains a.
Strong ownership / binding.
Parts belong to one whole.
Parts do not change during execution.
Automobile

public class Automobile


{

Motor

Transmission

private final Transmission drive = new Transmission();


private final Motor engine = new Motor();
}
23

Composition Example 1 A Person has a name


and an address
Person
Person(Name nama, Address add)
Name getName()
Address getAddress()
String toString()

Name
firstName : String
lastName : String
Name(String first, String last)
String getFirstName()
String getLastName()
String getFullName()

Address
street : String
city : String
state : String
zip : String
Address(String street, String city, String state, String zip)
String getStreet()
String getCity()
String getState()
String getZip()
String getFullAddress()

24

Composition Example 1 Name class

public class Name

private String firstName;


private String lastName;
public Name(String firstName, String lastName)

this.firstName=firstName;
this.lastName=lastName;

}
public String getFullName()
{

return firstName + ' ' + lastName; } }


25

Composition Example 1 Address class

public class Address

private String street;

private String city;

private String state;

private String zip;

public Address(String street, String city, String state, String zip)


{

this.street = street;
this.city = city;
this.state = state;
this.zip = zip;

public String getFullAddress()


{
}

return street +'\n' + city+ ", " +state+ ' ' +zip ; }
26

Composition Example 1 Person has a name


and an address
public class Person
{

private Name name;


private Address address;
public Person(Address1 address)
{
name1=new Name("Mohamad", "Ali");
this.address=address;
}
public Name getName()
{

return name; }

public String toString()


{
}

return '\n' + name.getFullName() + '\n'


+address.getFullAddress() +'\n'; }
27

Composition Example 1 TestPerson


public class TestPerson {
public static void main(String args[]){
Address add1 = new Address("Jalan Pulai 13/2",
"Skudai", "Johor", "81310");
Person per1 = new Person(add1);
System.out.println(per1);
}
}

28

Self-Test
What is the appropriate relationship

for the following classes:


Company and Employee
Faculty and Course
Customer and Product
Student and Matric Number
Employee and Address
House and Door

31

You might also like