0% found this document useful (0 votes)
2 views

4. Some Techniques in Class Building

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

4. Some Techniques in Class Building

Uploaded by

Đỗ Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4.

Some techniques in class building


1. Method overloading
Each method has it own signature

Method signature

Method overloading

Methods in class can have the same name, if they have different signatures
(different number of arguments or different types of arguments)

Advantages

easier for devs since they don't have to remember too many method names.

Methods are considered as overloading only if they belong to the same


class.

Only apply this technique on methods describing the same kind of task

Method overloading can be applied to Constructor

this keyword

Refer to the current obj, used inside the class of the obj that it refers to.
use attributes or methods of object through . operator
Example

public class BankAccount{


private String owner;
public void setOwner(String owner){
this.owner = owner;
}
public BankAccount() {
this.setOwner(“noname”);
}

}

2. Class members and constant members


Constant members

Attribute/method that can not change its values/content during the usage.

access_modifier final data_type CONSTANT_VAR = value;

Constants associated with a class are declared as static final

Class members
Members belong to:
The whole class (static)
Individual (instance variables and methods)
Java Modifiers

Static attributes and methods

belong to the class, declared with static modifier

Changing a value in one obj changes the val for all of the objs

Can be accessed without instanting the class


Instance member Class member
Attributes/methods can only be Attributes/methods can be accessed
accessed via obj through class
Each obj has it own copy of an obj's All objects have thhe same copy of
attribute class attributes

Values of an attribute of different Values of a class attribute of different


objects are different objs are the same.

Caution

Static methods can access only staic attributes and can call static methods
in the same class.

3. Passing arguments to methods


Can pass primitive data types (int, double, char...) or references (array and
obj)

Variable Arguments
An arbitrary number of arguments, called varargs (nói đơn giản là cho nhập
số lượng ko giới hạn các @param có chung dạng dữ liệu)
Read more about Java Varargs

Declare an varargs method

<Data_type>...<param_name>

public int sum(int ... nums){


int result = 0;
for (int i = 0; i < nums.length; i++){
result += nums[i];
}
return result;
}

sum();// return 0
sum(1,2)//return 3
sum(1, 2, 3, 4, 5) //return 15
Caution

Varargs must be the last @param


There is only one varargs passed in a method

Passing by values

Info

Java passes all arguments to a method in form of pass-by-value (Passing


value/copy of the real argument)

With value-based date type

Primitive values can not be changed when being passed as a @param

public void method2(int a){


a = a +2;
}

public void method1(){


int a = 0;
System.out,println(a); // print out 0
method2(a);
System.out.println(a);// print out 0
}

With reference-based data type

Pass the references by value, not the original reference or the object
But what if we want to swap two Object ?

We call out a wrapper class

// Java program to Demonstrate that Wrapper Classes


// Can be Used to Swap two Objects

// Class 1
// A car with model and no.
class Car {
// Attributes associated with car
int model, no;

// Constructor of class 1
Car(int model, int no)
{
// This refers to current instance itself
this.model = model;
this.no = no;
}

// Method
// To print object details
void print()
{
System.out.println("no = " + no
+ ", model = " + model);
}
}

// Class 2
// Wrapper over class that is used for swapping
class CarWrapper {
Car c;

// Constructor
CarWrapper(Car c) { this.c = c; }
}

// Class 3
// Uses Car class and swaps objects of Car
// using CarWrapper
class GFG {
// This method swaps car objects in wrappers
// cw1 and cw2
public static void swap(CarWrapper cw1, CarWrapper cw2)
{
Car temp = cw1.c;
cw1.c = cw2.c;
cw2.c = temp;
}

// Main driver method


public static void main(String[] args)
{
Car c1 = new Car(101, 1);
Car c2 = new Car(202, 2);
CarWrapper cw1 = new CarWrapper(c1);
CarWrapper cw2 = new CarWrapper(c2);
swap(cw1, cw2);
cw1.c.print();
cw2.c.print();
}
}

You might also like