0% found this document useful (0 votes)
41 views31 pages

19 ClassRelationships

The document discusses different types of relationships between classes including association, aggregation, composition, and inheritance. It provides examples of association relationships where a student can register for multiple courses and a lecturer can teach multiple courses. It also presents a bidirectional association between student and course where a student can register for a course and a course can add students.
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)
41 views31 pages

19 ClassRelationships

The document discusses different types of relationships between classes including association, aggregation, composition, and inheritance. It provides examples of association relationships where a student can register for multiple courses and a lecturer can teach multiple courses. It also presents a bidirectional association between student and course where a student can register for a course and a course can add students.
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/ 31

Class Relationships

Lecture 18
Based on Slides of Dr. Norazah Yusof

1
Relationships among Classes
 Association
 Aggregation
 Composition
 Inheritance

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

Take Teach
5..60 0..3 1
Student * Course Teacher
Faculty

public class Student { public class Course { public class Faculty {


/** Data fields */ /** Data fields */ /** Data fields */
private Course[] private Student[] private Course[]
courseList; classList; courseList;
private Faculty faculty;
/** Constructors */ /** Constructors */
/** Methods */ /** Constructors */ /** Methods */
} /** Methods */ }
}

An association is usually represented as a data field in the


class. 3
Association

 Example: Student register course.

register(Course)
5..60 *
Student 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 Course
-matrix : String -name : String
-courseList : Course[] registerCourse(Course) -code : String
-numOfStudent : int - section : int
5..60 *
+ Student(String, String,
String) + Course(String, String,
+ getName() : String int)
+ getMatrix(): String + getName() : String
+ getNumOfStudent(): int
+ registerCourse(Course) public class Course
: void {/*attributes*/
+ printAllInfo():void /*methods*/}
public class Student
{ /*attributes*/
private Course[] courseList; 5

/*methods*/}
Association

• The relationship allows objects to call methods in other


objects.
Student
-name : String Course
-matrix : String -name : String
-courseList : Course[] registerCourse(Course) -code : String
-numOfStudent : int - section : int
5..60 *
+ Student(String, String,
String) + Course(String, String,
+ getName() : String int)
+ getMatrix(): String + getName() : String
+ getNumOfStudent(): int
+ registerCourse(Course) public class Course
: void {/*attributes*/
+ printAllInfo():void /*methods*/}
public class Student
{ /*attributes*/
private Course[] courseList; 6

/*methods*/}
Association

 This is the same as an object sending another


object a message.
 Therefore, it is implemented as object
references.
Student
-name : String Course
-matrix : String -name : String
-courseList : Course[] registerCourse(Course) -code : String
-numOfStudent : int - section : int
5..60 *
+ Student(String, String,
String) + Course(String, String,
+ getName() : String int)
+ getMatrix(): String + getName() : String
+ getNumOfStudent(): int
+ registerCourse(Course)
: void 7

+ printAllInfo():void
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 } 8

21 }
Association Example 1: Student register Course
(Student class)
1 public class Student {
2 private String name;
3 private String matrix;
4
5 private Course[] courseList; //showing association
6 private int numOfCourse;
7 public Student(String n,String m,String c){
8 name=n;
9 matrix=m;
10
11 courseList = new Course[10]; //showing association
12 }
13 public String getName() {
14 return name;
15 }
16
17 public String getMatrix() {
18 return matrix;
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
Course -courseList : Course[]
-name : String -numOfCourse : int
-code : String Teach(Course)
- section : int
0..3 1
+ Lecturer (String)
+ Course(String, String, + getName() : String
int) + getNumOfCourse(): int
+ getName() : String + teach(Course): void
+ printAllInfo():void
public class Course
{/*attributes*/
/*methods*/} public class Lecturer
{ /*attributes*/
private Course[] courseList;
12
/*methods*/}
Association Example 3: Bidirectional Relationship
Student register Course and Course add Students

Student
-name : String
-matrix : String Course
-courseList : Course[] register(Course) -name : String
-numOfStudent : int 5..60 * -code : String
+ Student(String, String, 0..3 1 - section : int
add(Student)
String) - stuList: Student[]
+ getName() : String + Course(String, String,
+ getMatrix(): String int)
+ getNumOfStudent(): int + getName() : String
+ registerCourse(Student): + addStudent(Course):
void void
+ printAllInfo():void + printAllInfo():void
public class Student public class Course
{ /*attributes*/ {/*attributes*/
private Course[] courseList; private Student[] stuList; 13

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

41 }
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.

1
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 Aggregation

Name Person Address

19
Representing Aggregation in Classes

An aggregation relationship is usually


represented as a data field in the aggregated
class.
public class Name { public class Person { public class Address {
/** Data fields */ /** Data fields */ /** Data fields */
/** Constructors */ private Name name; /** Constructors */
/** Methods */ private Address address; /** 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
FSKSMBuilding

Lab LectureRoom LecturerRoom


labNo : String lectureNo : String room No : String

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) Address


String getFirstName() street : String
String getLastName() city : String
String getFullName() 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(Address address)


{
name=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
Inheritance

Inheritance models the is-an-extension-of


relationship between two classes.

public class Faculty extends Person {


/** Data fields */
Person Faculty
/** Constructors */
/** Methods */
}
(A) (B)

29
Weak Inheritance Relationship
 A weak is-an-extension-of relationship can be represented using
interfaces.
 For example, the weak is-an-extension-of relationship “students
are comparable based on their grades” can be represented by
implementing the Comparable interface, as follows:

public class Student extends Person


implements Comparable {
Person /** Data fields, Constructors, and */
Student /** Methods */
Comparable
/** Implement the compareTo method */
public int compareTo(Object object) {
// ...
}
}
30
(A) (B)
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