Untuk
membuat method overloading diperlukan setidaknya satu dari empat syarat di
bawah ini:
- Jumlah parameter berbeda, meskipun tipe data
parameternya sama.
- Tipe data parameter berbeda meskipun jumlahnya sama.
- Jumlah dan tipe data parameter berbeda.
- Tipe nilai kembalian method atau konstruktor harus sama.
1. Data Karyawan
package overloading;
public class DataKaryawan {
private String nama;
private String nip;
private String noTelpon;
public String status = "karyawan
wrexy.inc";
private double gaji;
private double persenNaikGaji = 0.15;
publicDataKaryawan(String nama, String nip) {
this.nama = nama;
this.nip = nip;
}
publicDataKaryawan(String nama, String nip, String
status){
this.nama = nama;
this.nip = nip;
this.status = status;
}
public void setNama(String nm){
nama = nm;
}
public void setNip(String no){
nip = no;
}
public void setTelpon(String noTlp){
noTelpon = noTlp;
}
public void setStatus(String status){
this.status = status;
}
public void setGaji(double gj){
gaji = gj;
}
public void setPersenNaikGaji(double persen){
persenNaikGaji = persen;
}
public String getNama(){
returnnama;
}
public String getNip(){
return nip;
}
public double getGaji(){
gaji = gaji + (gaji*persenNaikGaji);
returngaji;
}
public String getTelpon(){
returnnoTelpon;
}
public String getStatus(){
return status;
}
}
2.
Data
CEO
package overloading;
public class DataCeo extends
DataKaryawan{
private String status =
"CEO";
private double hasilUsaha =
10000000;
publicDataCeo(String nama,
String nip){
super(nama, nip);
}
publicDataCeo(String nama,
String nip, String status){
super(nama, nip, status);
}
publicDataCeo(String nama,
String nip, double hasilUsaha){
super(nama, nip);
this.hasilUsaha = hasilUsaha;
}
public void
setHasilUsaha(double hasilUsaha){
this.hasilUsaha = hasilUsaha;
}
public String getStatus(){
returnthis.status;
}
public double getGaji(){
super.setGaji(0.1 *
hasilUsaha);
returnsuper.getGaji();
}
}
3.
Data Manager
package overloading;
public class DataManager
extends DataKaryawan {
public String status =
"manajer";
private double bonus;
publicDataManager(String
nama, String nip){
super(nama,nip);
}
publicDataManager(String
nama, String nip, String status) {
super(nama, nip, status);
}
public void setStatus(String
status){
this.status = status;
}
public void
setStatus(inttipe){
switch(tipe){
case 1:
this.status =
"manajeroperasional";
break;
case 2:
this.status =
"manajerkeuangan";
break;
case 3:
this.status = "manajer
marketing";
break;
case 4:
this.status =
"manajerpersonalia";
break;
default:
this.status =
"manajer";
}
}
public void setBonus(double
bns){
bonus = bns;
}
public String getStatus(){
returnthis.status;
}
public void setGaji(double
gaji){
super.setGaji(gaji +
(gaji*bonus));
}
public double getGaji(){
returnsuper.getGaji();
}
}
4.
TesDataKaryawan
package overloading;
public class TesDataKaryawan {
public static void main(String[] args) {
DataKaryawankaryawan;
DataCeo data2 = new DataCeo("Zoelfata","CEO007195",10000000);
data2.setGaji(30000000);
data2.setTelpon("08126558xxxx");
System.out.println("Nama :"+data2.getNama());
System.out.println("Nip :"+data2.getNip());
System.out.println("Gaji :"+data2.getGaji());
System.out.println("No Telpon :"+data2.getTelpon()+"\n");
DataManager data1 = new DataManager("Fata","MOP007195");
data1.setStatus(1);
data1.setGaji(1000000);
data1.setTelpon("08526126xxxx");
data1.setPersenNaikGaji(0.5);
data1.setBonus(0.5);
System.out.println("Nama :"+data1.getNama());
System.out.println("Nip :"+data1.getNip());
System.out.println("Status :"+data1.getStatus());
System.out.println("Gaji :"+data1.getGaji());
System.out.println("No Telpon :"+data1.getTelpon()+"\n");
}
}
5.
Output
Program
0 komentar:
Posting Komentar