Relationships among Classes - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Relationships among Classes

Description:

Association represents a general binary relationship that ... The arrow is optional and specifies navigability. No arrow implies bidirectional navigability. ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 30
Provided by: radz
Category:

less

Transcript and Presenter's Notes

Title: Relationships among Classes


1
Relationships among Classes
  • Association
  • Aggregation Composition
  • Inheritance

2
Association
  • Association represents a general binary
    relationship that describes an activity between
    two classes.
  • The relationship allows objects to call methods
    in other objects.
  • This is the same as an object sending another
    object a message.
  • Therefore, it is implemented as object
    references.
  • The arrow is optional and specifies navigability.
  • No arrow implies bidirectional navigability.

3
Association 1st. Case
  • An association is usually represented as an
    attribute / a data field in the class.

public class Student Subject subjnew
Subject5 /attributes/ /methods/
public class Subject /attributes/ /methods/
4
Association 1st. Case Example
import java.util. class Association public
static void main(String args) Subject subj
new Subject5 subj0 new Subject("OOP","SCP3
103",03) subj1 new
Subject("DATA STRUCT","DCP2023",01) Student s1
new Student("ali","DC0021","2DCK") Student s2
new Student("abu","DC0022","3DCK") Student s3
new Student("ben","DC0023","3SCK") s1.regSubje
ct(subj) s2.regSubject(subj) s3.regSubject(sub
j) s2.regSubject(subj) s1.printAllInfo() s2.
printAllInfo() s3.printAllInfo()
5
Association 1st. Case Example (cont.)
class Student String name String
matrix String course Student(String
n,String m,String c) namen matrixm cour
sec public String getName() return
name public void regSubject(Subject subj)
//showing association printAllInfo(subj)
6
public void printAllInfo(Subject
subj) System.out.println("\nSTUDENT NAME
"name) System.out.println("NUMBER
OF SUBJECT(s) TAKEN "subj.size()) System.o
ut.println("LIST OF SUBJECT(s) TAKEN
") for(int i0iltsubj.size()i) System.o
ut.println(subji.getName()) //Student
7
class Subject String name String code int
section Subject(String n,String c,int
s) name n code c section
s public String getName() return
name //Subject
8
Association 2nd. Case
public class Subject Student studnew
Student5 /attributes/ /methods/
public class Student /attributes/ /methods/
9
Association 2nd. Case Example
import java.util. class Association public
static void main(String args) Subject subj1
new Subject("OOP","SCP3103",03) Subject
subj2 new Subject("DATA STRUCT","DCP2023",01)
Student snew Student5 s0 new
Student("ali","DC0021","2DCK") s1 new
Student("abu","DC0022","3DCK") s2 new
Student("ben","DC0023","3SCK") subj1.addStudent
(s) subj1.addStudent(s) subj1.addStudent(s)
subj2.addStudent(s) subj1.printAllInfo() s
ubj2.printAllInfo()
10
Association 2nd. Case Example (cont.)
class Subject String name String code int
section Subject(String n,String c,int
s) name n code c section
s public String getName() return
name public void addStudent(Student
stud) PrintAllInfo(stud)
11
public void printAllInfo(Student
stud) System.out.println("\nSUBJEK
"name) System.out.println("NUMBER OF
STUD REGISTERED "stud.size()) System.out.pr
intln("LIST OF STUD REGISTERED ") for(int
i0iltstud.size()i)
System.out.println(si.getName()) //Su
bject
12
class Student String name String
matrix String course Student(String n,String
m,String c) namen matrixm coursec
public String getName() return
name //Student
13
Association 3rd. Case
  • This is a bi-directional association.

public class Student Subject subjListnew
Subject5 /attributes/ /methods/
public class Subject Student studListnew
Student5 /attributes/ /methods/
14
Association 3rd. Case Example
import java.util. class Association4 public
static void main(String args) Subject subj1
new Subject4("OOP","SCP3103",03) Subject
subj2 new Subject4("DATA STRUCT","DCP2023",01)
Student s1new Student5 s10 new
Student4("ali","DC0021","2DCK") s11 new
Student4("abu","DC0022","3DCK") s32 new
Student4("ben","DC0023","3SCK") subj1.addStuden
t(s1) subj2.addStudent(s1
) subj1.printAllInfo() subj2.printAllInfo(
) s10.printAllInfo() s11.printAllInfo()
s12.printAllInfo()
15
class Subject String name String code int
section Subject4(String n,String c,int
s) name n code c section
s public String getName() return
name public void addStudent(Student
stud) printAllInfo(stud) /make 2-way
association/ for(int i0 iltstud.lengthi)
studi.regSubject(this) //addStudent
16
public void printAllInfo(Student
stud) System.out.println("\nSUBJEK
"name) System.out.println("NUMBER OF
STUD REGISTERED "stud.length()) System.out.
println("LIST OF STUD REGISTERED
") for(int i0iltstud.lengthi)
System.out.println(si.getName())
17
class Student String name String
matrix String course Student(String n,String
m,String c) namen matrixm coursec
public String getName() return
name public void regSubject(Subject
subj) printAllInfo(subj) //regSubject
18
public void printAllInfo(Subject
sub) System.out.println("\nSTUDENT NAME
"name) System.out.println(sub.getName
())
19
Aggregation
  • A special form of association.
  • Represents an ownership relationships between two
    classes.
  • Models the relationship like has-a, part-of,
    owns.
  • Parts exist independent of whole.
  • Parts and whole have independent lifetimes.
  • Parts may belong to multiple wholes.
  • Parts may change during execution
  • Anti-symmetric (i.e. one way)
  • If A is part of B, B cannot be part of A.

20
Aggregation Example
public class FSKSMBuilding private Lab
lab1 private LectureRoom lect private
LecturerRoom lectr
21
Aggregation Example
class Lab public String labNo class
LectureRoom public String lectureNo class
LecturerRoom public String roomNo class
FSKSMBuilding Lab makmal LectureRoom
bilikKuliah LecturerRoom bilikPensyarah public
FSKSMBuilding(String _labNo,String _lectureNo,
String _roomNo) makmal new
Lab() bilikKuliah new LectureRoom() bilikP
ensyarah new LecturerRoom() makmal.labNo
_labNo bilikKuliah.lectureNo
_lectureNo bilikPensyarah.roomNo _roomNo
22
class TestAggregate public static void
main(String args) FSKSMBuilding fsksmnew
FSKSMBuilding("KPU8","BK2","306-02") System.out.
println("Makmal OOP ialah di "
fsksm.makmal.labNo "\n" "Bilik Kuliah di ""
"fsksm.bilikKuliah.lectureNo "\n""Bilik
Pensyarah di " fsksm.bilikPensyarah.roomNo)

23
Aggregation Example 2
24
Aggregation Example 2
public class Name private String
firstName private String lastName public
Name(String firstName, String lastName) this.fi
rstNamefirstName this.lastNamelastName p
ublic String getFirstName() return
firstName public String getLastName() return
lastName public String getFullName() return
firstName ' ' lastName
25
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.streetstreet this.citycity th
is.statestate this.zipzip public String
getStreet() return street public String
getCity() return city public String
getState() return state public String
getZip() return zip public String
getFullAddress() return street '\n' city ",
" state ' ' zip
26
public class Person private Name name private
Address address public Person(Name name,
Address address) this.namename this.address
address public Name getName() return
name public Address getAddress() return
address public String toString() return
'\n' name.getFullName() '\n' address.getFullAdd
ress() '\n'
27
public class TestPerson public static void
main(String a) Name nama new
Name("ALI", "Ahmad") Address add new
Address("Jalan A","Skudai","Johor","81300") Per
son persona new Person(nama, add) System.out.
println(persona.toString())
28
Composition
  • Form of aggregation.
  • For a relationship contains a.
  • Strong ownership / binding.
  • Parts belong to one whole.
  • Parts do not change during execution.

public class Automobile private final
Transmission drive new Transmission() private
final Motor engine new Motor()
29
The this Reference
  • Permits objects to refer to themselves.
  • Secret argument passed to each non-static
    method.
  • Points to the calling object binds its data to
    the method.
  • Used for self reference.

public class Circle int radius100 public
Circle(int radius) this.radiusradius public
Circle() this(100) public void draw()
  • this(..) as the first line in a constructor is a
    call to another constructor in the same class
    (distinguished by signature).
  • this is used to access instance variables when
    the name is hidden by a more local variable.
Write a Comment
User Comments (0)
About PowerShow.com