You are on page 1of 23

TOPIC 4: CLASSES – INTERMEDIATE (PART 1)

CSC 435 OBJECT ORIENTED PROGRAMMING


CHAPTER OUTLINE

 METHOD OVERLOADING
 OBJECTS AS PARAMETER
 OBJECTS AS METHOD TYPE
METHOD OVERLOADING

 Java Programming language support overloading method


 Java can distinguish between methods with different method signatures
 It means that, method within a class can have the same name if they have
different parameter list
 They are differentiated by number and type of the arguments passed into
method
METHOD OVERLOADING

 Methods can share the same name as long as


❖ They have different number of parameter Rule 1
❖ Their parameters are of different data types when the number of parameters is the same

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:

public class Shape{


private static final PI = 3.14;
//area for rectangle
Share same method’s
public int calArea(int length, int width) {
return (length * width);
name calArea
}
//area for triangle
public int calArea(float base, float height) { Different number of
return (1/2 * base * height); parameters and data
} types
//area for circle
public int calArea(double radius) {
return (PI * pow(radius,2);
}
}
METHOD OVERLOADING - EXAMPLE 2

public class DataArtist{


…;
public void draw (String s) {…}
public void draw (int i) {…}
public void draw (double f) {…}
public void draw (int i, double f) {…}
}

Method overloading due to different in number of


parameter or parameter type or both.
METHOD OVERLOADING - EXAMPLE 3 (DIFFERENT ORDER OF
ARGUMENT)
❖ Following example is overloading based on the order of the arguments.

public class MainClass{


//method 1 Output
static void print(String s, int i) {
System.out.println(“String : “ + s + “, int :
“ + i);
}
//method 2
static void print( int i, String s) { String : String first, int : 11
Int :99, String : int first
System.out.println(“int : “ + i + “, String :
“ + s);
}
public static void main (String[] arg) {
print(“String first “, 11);
print(99, “ int first”);
}
METHOD OVERLOADING - EXAMPLE 4 (DIFFERENT PARAMETER
DATA TYPE)

public class MainClass{


//method 1
public int printNumber (int i) {
return i * 2;
}
//method 2
Different parameter
public double printNumber (double i) {
return i + 3; data type
}

public static void main (String [] arg) {


printNumber(3); //will invoke
method 1
printNumber(3.0); //will invoke
method 2
}
METHOD OVERLOADING - EXAMPLE 5 (NOT A METHOD
OVERLOADING)

public int countRows (int number);


public String countRows (int number);

❖This code will return error when we try to compile.


❖The compiler does not consider return type when differentiating methods.
❖Therefore two method cannot be declared with the same signature
even if they have different return type.
OVERLOADED CONSTRUCTOR

❖The same rules apply for overloaded constructor.


❖This is how we can define more than one constructor to a class
CONSTRUCTOR AND ORDINARY METHOD OVERLOADING-
EXAMPLE 6
class MyClass { Main method
int height;
MyClass() { public class MainClass {
S.O.P (“Planting a seedling”);
Height = 0; public static void main(String[ ] args) {
} MyClass t = new MyClass(0);
MyClass(int i) { t.info();
S.O.P (“Creating new Tree that is “ + i + “ t.info(“overloaded method”);
feet tall”); MyClass t2 = new MyClass();
height = i; }
} }
void info() {
S.O.P (“Tree is ” + height + “ feet tall”);
} Output:
void info(String s) { Creating new Tree that is 0 feet tall
S.O.P (s + “ : Tree is “ + height + “ feet
tall”); Tree is 0 feet tall
} overloaded method : Tree is 0 feet tall
Cont.. Planting a seedling
OBJECTS AS PARAMETER

❖There are two type of passing parameter in Java:

Pass by Value Pass by Reference


- For passing primitive data type - For passing object type
- Can change that copy inside the - Can change that copy inside the
method, but this will have no method, and have an effect on
effect on actual parameter actual parameter.
PASS BY VALUE: EXAMPLE 7

public void modify (int data) //method modify


int x = 10; //stored in stack
{
Int data = 10; //stored in stack
data = 20;
Data = 20; //no impact on “x”
}

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

Value passed from class


Planet
PASS BY REFERENCE: EXAMPLE 9

public void modify (rectangle r2) //method modify


Output :
{
20
r2.length = 20;
}

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

Received object Player


Received object Person
through parameter
through parameter
OBJECT AS METHOD TYPE

• 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

public class Emp In application class:


{
private int empID; //call normal constructor
private String name; Emp emp = new Emp(101,”Ali”);
System.out.println(emp.toString( ));
public Emp(int I, String n) {……}
public int getEmpID( ) {…..} //call method getEmp( )
public String getName( ) {….} Emp newEmp = emp.getEmp( );
Public String toString( ) {……} System.out.println(newEmp.toString( ));

public Emp getEmp()


{
Emp e = new Emp (111,”Ah Chong”); Output:
return e;
} 101 Ali
} 111 Ah Chong
EXERCISE
Identify whether the method is valid or not. Give a reason for each of the questions:
1. int Azalea (int a, int b) and int Azalea (int a, int b, int c)
2. int Azalea (int a, int b) and double Azalea (double a, double b)
3. int Balsam (int a, int b) and double Balsam (int a, int b)
4. void Begonia (int a, long b) and Void Begonia (int a, int b, int c)
5. int Cosmos (int a, int b) and float Cosmos (int var1, int var2)
6. void Fennel (char c) and void Fennel (int c)
7. void Iris (char c, int num) and void iris (int num, char c)
8. int Lavender (int a, int b, int c) and int Lavender (int var1, int var2, int
var3)
9. int mango (int a, int b) and int mango(float a, float b)

You might also like