Topic 4 - Classes (Intermediate) (Part 1)
Topic 4 - Classes (Intermediate) (Part 1)
METHOD OVERLOADING
OBJECTS AS PARAMETER
OBJECTS AS METHOD TYPE
METHOD OVERLOADING
Rule 2
EXAMPLE
// Overloaded max()method to determine the maximum value between two integers
public int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
// Overloaded max()method to determine the maximum value between two double numbers
public double max(double num1, double num2)
{
if (num1 > num2)
return num1; Note: The java compiler
else determines which method to
return num2; call/invoke based on the method
} signature.
METHOD OVERLOADING - EXAMPLE 1
Method overloading for calculating the Area of Shape:
Main OUTPUT
int x = 10; //Primitive data 10
modify(x); //call method modify & pass x
as parameter
System.out.print(x);
PASS BY VALUE: EXAMPLE 8
Main r1
rectangle r1 = new rectangle() //object reference type Memory
r1.length = 10;
modify(r1); //pass r1 as parameter
r2
System.out.print(r1.length);
PASS BY REFERENCE: EXAMPLE 10
• Instead of using primate data types or void, object can be also used as a method type, means
Object can also be used as a return value in a method
• Object methods, also known as subprograms, are functions or procedures that you can
declare in an object type definition to implement behavior that you want objects of that
type to perform.
[AccessModifier] [object datatype]
[method name] ([argument])
{
Statement;
Return object;
}
EXAMPLE 11
Method
getScore()
EXAMPLE 11: (CONT)
Score
=70
s1
Student
s2
Score
=80
Student
EXAMPLE 11: (CONT) – AFTER CALL HIGHEST() FUNCTION:
Score=70 Student
s1
s2
Score=80
Student
sHigh
EXAMPLE 12