Topic 4 Part 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

CSC186

OBJECT ORIENTED
PROGRAMMING
Topic 4:
Classes – Intermediate (Part 2)
TOPIC COVERED

 Composite objects
 Objects as parameter

 Object as method type


COMPOSITE
OBJECT
COMPOSITE OBJECTS

 Relate two classes.

 One or more members of a class are objects of another class


type

 “has-a” relation

 Example: “every person has a date of birth.”

 Composite object include class objects as instance variables


of other classes.
EXAMPLE COMPOSITE CLASS 1

public class Person


{
private String name;
private String ic;
private Date dob;

.
.
.
}
EXAMPLE COMPOSITE CLASS 2

public class Personal


{
private int day;
private int month;
private int year;
private Student stu; // String sName, int sId

.
.
.
}
a) Example of normal constructor that has a composite object :

public Personal (int dDay, int dMonth, int dYear, String sName, int sId)
{
day = dDay;
month = dMonth;
year = dYear;
stu = new Student (sName, sId);
}
b) Example of mutator that has a composite object :

public void setPersonal (int dDay, int dMonth, int dYear, int sId, String sName)
{
day = dDay;
month = dMonth;
year = dYear;
stu.setStudent(sName, sId);
}

b) Example of accessor that has a composite object :

public String getStudentName( )


{
return stu.getName( );
}
IMPORTANT

 You have to define inner class before you can define


outer class.
2
2
Class : Personal
Class : Person
Attribute :
Attribute : -day (int)
- name (String) -month (int)
- ic (String -year (int)
1 1
- dob (Date)
- stu (Student)
TEST YOURSELF 2 – COMPOSITE
OBJECT
1. Create a new class named Agent with name,
contact number as the attributes with the basic
methods.

2. Modify the Actor class to add the Agent


information (including mutator and retriever for
the Agent)

3. Modify Test Yourself 1 to ActorArrayApp2 to


allow user manipulate the modified Actor class.
OBJECT AS
PARAMETER
& RETURN TYPE
OBJECTS AS PARAMETER

 We can also pass an object to a method (object as parameter).

 Like passing an array, passing an object is actually passing the


reference (name) of the object.
PASSING OBJECTS TO METHODS
Example of mutator that receive an object :

public void setPersonal (int dDay, int dMonth, int dYear, Student ss)
{
day = dDay;
month = dMonth;
year = dYear;
stu = ss;
}
SHARING AN OBJECT

We pass the same Student object to card1 and card2


SHARING AN OBJECT

Since we are actually passing a reference to the same


object, it results in the owner of two LibraryCard
objects pointing to the same Student object
OBJECT AS METHOD (RETURN) TYPE

 Returna reference type (object)


 Sample codes:

// A method returns a reference type object

public Students highest(Students stud1, Students stud2)


{

if (stud1.getScore() > stud2.getScore())


return stud1;
else
return stud2;
}
Example of accessor that return an object :

public Student getStudent( )


{
return stu;
}
END.
Thank you.

You might also like