Topic 3:
Class
(Intermediate)
CSC435: OBJECT ORIENTED PROGRAMMING
Prepared by Siti Norsyuhada Zainudin
Content
Method overloading
Objects as parameter
Object as method / return type
Array of objects
Composite objects
Application
Reserve Word - this
Reserve Word - this
The reserved word this is called a self-referencing pointer because it
refers to an object from the object's method.
It can be used to refer to the current object and also data member.
It is used to avoid naming confusion in the method or constructor
Reserve Word – this : Example 1
public Fraction add( Fraction frac) {
int a, b, c, d;
Fraction sum;
Using this to call a a = this.getNumerator();
method of a b = this.getDenominator();
receiving object c = frac.getNumerator();
d = frac.getDenominator();
sum = new Fraction(a*d + b*c, b*d);
return sum;
}
Reserve Word – this : Example 2
class Person
{
int age;
Using this to public void setAge(int age)
refer to a data {
member this.age = age;
}
...
}
Reserve Word – this : Example 3
public Fraction( ){
this(0.1);
}
public Fraction(int number) {
this(number, 1);
}
Using this to call a constructor public Fraction(Fraction frac) {
from another constructor of the this(frac.getNumerator();
same class frac.getDenominator());
}
public Fraction(int num, int denom) {
setNumerator(num);
setDenominator(denom);
}
Method Overloading
Method Overloading
Multiple method with the same name but different parameter
Two ways to overload methods
Change number of parameters Change data type
Class method{ Class method{
Public void methodTest() Public int methodTest(int a, int b)
{…} {…}
Public void methodTest(int a, double b) Public double methodTest(double a, double b)
{…} {…}}
Public void methodTest(double b, int a)
{…}
Public void methodTest(int a, double c, char ch)
{…}}
Method Overloading
Method Overloading Result Reason
int Strawberry (int a, int b, float c) Invalid Same parameter
int Strawberry (int var1, int var2, float var3)
int Grapes(int a, int b) Valid Different types of parameter
int Grapes(float var1, float var2)
int Orange(int a, int b) Valid Different number of parameter
int Orange(int num)
float Kiwi(int a, int b) Valid Different sequence of data type
float Kiwi(float var1, int var2)
int Mango(int a, int b) Invalid Same parameter
float Mango(int var1, int var2)
Int Mango(int a, int b) Valid Different data type and different
Float Mango(float a, float b) parameter
Method Overloading: Example 1
public class Shape{
private static final PI = 3.14;
//area for rectangle //area for circle
public int calArea(int length, int width) { public int calArea(double radius) {
return (length * width); return (PI * pow(radius,2);
} }
//area for triangle }
public int calArea(float base, float height) {
return (1/2 * base * height);
}
Method Overloading: Example 2
Class definition Main application
class overload{
... Public class mainClass{
public static int sum(int x, int y){ Public static void main(String[] args){
return x+y; } System.out.println(sum(5,9));
Public static int sum(int x, int y, int z){ System.out.println(sum(4,3,8));
return x+y+z; } }}
}
Object as Parameter
Types of Passing Object
Pass by Value Pass by Reference
For passing primitive data type. For passing object type.
Can change that copy inside Can change that copy inside
the method, but this will have the method, and have an
no effect on actual parameter effect on actual parameter
(argument) (argument)
Example: Pass by Reference
Class definition Main application
class Record { class main{
int num; public static void main(String[ ] args) {
String name;
Record id = new Record();
}
//Now we can pass a Record as a parameter to a method.
id.num = 2;
public static void tryObject(Record r) { id.name = “Barney”;
r.num = 100; tryObject(id);
r.name = “Fred”; System.out.println(id.num + “ “ +
} id.name);
} }}
Example: Pass by Value
Class definition Main application
class Tester{
class MyPoint{ public static void main (String[] args){
{
void myMethod( int var1, int var2 )
int var1 = 10;
{
int var2 = 20;
var1 = 0;
myMethod( var1, var2 );
var2 = 0;
System.out.println( "var1="+var1 +“
} var2="+var2 );
}}
Object as
Method/Return Type
Object as Method
Instead of using primate data types or void, object can be also used
as a method type.
[Access modifier] [object data type] [method name] ([argument])
{
Statement;
Return object;
}
Object as Method : Example 1
public Fraction simplify( )
{
int num = getNumerator();
int denom = getDenominator();
int gcd = gcd(num, denom);
Fraction simp = new Fraction (num / gcd, denom / gcd);
return simp;
}
Object as Method : Example 2
public Student determineHighest (Student stud1, Student stud2)
{
if (stud1.getScore() > stud2.getScore()
return stud1; //return the object
else
return stud2; //return the object
}
Array of Object
Array -Recap
A collection of data values
In Java, an array is an indexed collection of data values of the same
type
Arrays of primitive data types
Array of int [ ] 5 8 7 4 9
Array of char [ ] T K D P C
Array of String [ ] “Apple” “Mango” “Banana” “Orange” “Melon”
Array of Object
Create an array to store object type.
Passing array as parameter to method; only its reference is passed
Student Student Student Student Student
(name, id) (name, id) (name, id) (name, id) (name, id)
Array of Object - Syntax
class_name[] object_name = new class_name[size];
Student[] person= new Student[20];
Vehicle[] type= new Vehicle[10];
Clock[] currentTime= new Clock[5];
Array of Object
Clock[] currentTime= new Clock[5]; currentTime [0].setTime(8,15,47)
currentTime currentTime
hr 8
currentTime [0] currentTime [0] min 15
sec 47
currentTime [1] currentTime [1]
currentTime [2] currentTime [2]
currentTime [3] currentTime [3]
currentTime [4] currentTime [4]
Example
Composite of Object
Composite of Object
A way to combine simple objects or data types into more complex
ones.
One or more members of a class are objects of another class type.
“has-a” relation; for example, “every person has an address”
In other word, a class has object(s) of another class(es) as their
data members.
In composition, when the owning object is destroyed, so are the
contained objects
Composite of Object - Example
Application
Searching Inside Array of Objects
The most common operation that we use on array of
objects is searching.
We can search for:
Object
with highest/lowest value for any attribute/data
members.
Object
with specific value for any attribute/data
members.
Example
Our main program..
Finding student with highest score
You can search student with
minimum score just by
switching ‘>’ with ‘<‘ inside if
Finding student with specific name