Professional Documents
Culture Documents
Construction and Overloading: A. The Purpose
Construction and Overloading: A. The Purpose
Construction and Overloading: A. The Purpose
A. THE PURPOSE
Introduce the constraction, overloading constructor and overloading methode
in java term which is capable to gives initial values whe the object is being
created. Gives over view how the constructior works and called.
B. THEORETICAL BASIS
Java will call this constructor methode automaticaly when new is
used to create objects of a class.
Constructor type:
1.1.Create
new
java
file
named "Buku.java",
write program
code
as below.
public class Buku {
String pengarang;
String judul;
int tahunTerbit;
//constructor
public Buku(String pengarang, String judul){
this.judul=judul;
this.pengarang=pengarang;
}
//methode
public void infoBuku(){
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit);
}
}
1.2.Save and compile the codes.
1.3.Next create new class having main function as below.
public class Overloadingkonstruksi {
public static void main(String[]args){
Buku bukuku=new Buku("Dedi Gunawan","panduan pintar
kumputer",);
bukuku.infoBuku();
1.4.Save same as the name of the class and compile the code.
//constructor overloading
public Buku (String pengarang,String judul,int tahunTerbit){
this.pengarang=pengarang;
this.judul=judul;
this.tahunTerbit=tahunTerbit;
}
//methode
public void infoBuku(){
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit);
}
}
2.2.Compile the program code.
2.3.Next create new class named
"OverLoadingKonstruktor.java", and
new Buku();
other wise in other form.
New Buku(parameter 1, parameter 2);
A cunstructor creation mechanism which has more than one shape is differed
by the amount of the parameters. Overloading can be conduct for nonconstructor methode.
F. TASK
Edit Buku.java class and Overloadingkonstruksi.java. and the required
result as following picture (modification is allowed for judul buku, pengarang
dan tahun terbit).
Solution:
public class Buku {
String pengarang;
String judul;
int tahunTerbit;
String pemilik;
public Buku(String x,String pengarang, String judul){
pemilik=x;
this.judul=judul;
this.pengarang=pengarang;
}
public Buku (String pemilik,String pengarang,String judul,int
tahunTerbit){
this.pemilik=pemilik;
this.pengarang=pengarang;
this.judul=judul;
this.tahunTerbit=tahunTerbit;
}
public Buku (String pemilik,String pengarang){
this.pemilik=pemilik;
this.pengarang=pengarang;
}
public Buku (String pemilik,String judul,int tahunTerbit){
this.pemilik=pemilik;
this.judul=judul;
this.tahunTerbit=tahunTerbit;
}
public void infoBuku(){
System.out.println("pemilik "+pemilik);
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit+"\n");
}
public class Overloadingkonstruksi {
public static void main(String[]args){
Buku bukuku=new Buku("ku","panduan pintar kumputer","Dedi
Gunawan");
Buku bukuku1=new Buku("mu","panduan pintar kumputer","Dedi
Gunawan",2009);
Buku bukuku2=new Buku("kita","Dedi Gunawan");
Buku bukuku3=new Buku("kami","panduan pintar
kumputer",2009);
bukuku.infoBuku();
bukuku1.infoBuku();
bukuku2.infoBuku();
bukuku3.infoBuku();
}
}