MODUL 3
CONSTRUCTOR AND OVERLOADING
A. Objective
To understand
how was the constructor and overloading are running.
To compare
between constructor and overloading
B. Basic Theory
Constructor
Constructor is
a method can be used to give a start value when the object has been created.
This method called automatically by java when new is used to create an object
from a class.
Constructor
characteristic:
·
The name is
same with class name
·
Unavailable
backward value (include not allowed to have a void keyword)
Overloading
Constructor
Overloading to
constructor is a constructor making mechanism that have more than one shape.
The differentiator between one constructor with another is a sum of parameter
or parameter type.
Overloading
Method
Usually
overloading used in Konstruktor, but overloading also can be used in
non-konstruktor method
C. Tools and Materials
·
Laptop
·
NetBeans
Software
·
Java
Development Kit (JDK)
D. Steps
First
Experiment
1. Make a new java file named file “Buku.java”. Type a program code
below
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public
class Buku {
String pengarang;
String judul;
int tahunTerbit;
//membuat konstruktor
public
Buku(String pengarang, String judul){
this.judul = judul;
this.pengarang = pengarang;
}
//akhir konstruktor
//membuat method
public void infoBuku(){
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit);
}
}
2. Save and compile that program code
3. Make a new class that have main function () below
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public
class OverLoadingKonstruktor {
public static
void main (String []args){
Buku bukuku = new
Buku("Dedi
Gunawan","Panduan
Pintar Komputer");
bukuku.infoBuku();
}
}
4.
Save a new
class with class name and compile that program code. In that program object
creation can not use:
new Buku();
melainkan harus dalam bentuk
new
Buku(Parameter 1, Parameter 2);
Second Experiment
1.
Edit “Buku.java” file with adding overloading constructor
code as below
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public
class Buku {
String pengarang;
String judul;
int tahunTerbit;
//membuat konstruktor
public
Buku(String pengarang, String judul){
this.judul = judul;
this.pengarang = pengarang;
}
//akhir konstruktor
//Membuat overloading konstruktor
public
Buku (String pengarang, String judul, int tahunTerbit){
this.pengarang = pengarang;
this.judul = judul;
this.tahunTerbit = tahunTerbit;
}
//membuat method
public void infoBuku(){
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit);
}
}
2. Compile that profram code
3. Next, make a new class named “OverLoadingKonstruktor.java”. Write
down that code as below
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public class OverLoadingKonstruktor {
public static
void main (String []args){
Buku bukuku = new
Buku("Dedi
Gunawan","Panduan
Pintar Komputer");
Buku bukumu = new
Buku ("Dedi
Gunawan","Panduan
Pintar Komputer", 2009);
bukuku.infoBuku();
bukumu.infoBuku();
}
}
4. Save and compile that program code
5. Compare with the result before.
Third
Experiment
1. Make a new class named “Matematika.java”, write down the
programcode as picture below
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public
class Matematika {
static public double
kuadrat(double nilai){
return
nilai*nilai;
}
static public
int kuadrat(int nilai){
return
nilai*nilai;
}
static public double kuadrat(String
nilai){
double
bilangan;
bilangan= Double.valueOf(nilai).doubleValue();
return
bilangan*bilangan;
}
}
2. Compile that program code
3. Next, make a new class with main function (), as below
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public
class OverLoadingMethod {
public static
void main (String []args){
System.out.println("Nilai Kuadrat dari tipe data
double "+Matematika.kuadrat(20));
System.out.println("Nilai Kuadrat dari tipe data
integer "+Matematika.kuadrat(20));
System.out.println("Nilai Kuadrat dari tipe data
String "+Matematika.kuadrat(20));
}
}
4. Save file as class name and compile
Excercisses
1.
Edit Buku.java
class and OverLoadingKonstruktor.java class
·
Buku.java class
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public class Buku {
String tipe;
String pengarang;
String judul;
int tahunTerbit;
//membuat konstruktor
public
Buku(String tipe, String pengarang, String judul){
this.tipe = tipe;
this.judul = judul;
this.pengarang = pengarang;
}
//akhir
konstruktor
//Membuat overloading konstruktor
public
Buku (String tipe, String pengarang, String judul, int tahunTerbit){
this.tipe = tipe;
this.pengarang = pengarang;
this.judul = judul;
this.tahunTerbit
= tahunTerbit;
}
public
Buku (String tipe, String pengarang){
this.tipe = tipe;
this.pengarang
= pengarang;
}
public
Buku (String tipe,
String judul, int tahunTerbit){
this.tipe = tipe;
this.judul = judul;
this.tahunTerbit = tahunTerbit;
}
//membuat method
public void infoBuku(){
System.out.println(""+tipe);
System.out.println("Judul Buku : "+judul);
System.out.println("pengarang : "+pengarang);
System.out.println("tahun terbit : "+tahunTerbit);
System.out.println(" ");
}
}
·
OverLoadingKonstruktor.java
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author DESY
*/
public
class OverLoadingKonstruktor {
public static
void main (String []args){
Buku bukuku = new
Buku("Desyta
Zafranisa","Tanpa
Nama", 2013);
Buku bukumu = new
Buku ("Eddryn","Senyum Manis di Sore
gerimis", 2013);
Buku bukukita = new Buku ("Nicholas
Leonardo","Senja
di Ujung Jalan", 2012);
Buku bukukamu = new Buku ("Diandra
Yusuf","Dokumentasi abadi",
2013);
bukuku.infoBuku();
bukumu.infoBuku();
bukukita.infoBuku();
bukukamu.infoBuku();
}
}
2.
The final
result after the book title, author, and release year modified.
AFTER RUNNING:
Buku Ku
Judul Buku : Tanpa Nama
pengarang : Desyta Zafranisa
tahun terbit : 2013
Buku Ku
Judul Buku : Senyum Manis di Sore gerimis
pengarang : Eddryn
tahun terbit : 2013
Buku Ku
Judul Buku : Senja di Ujung Jalan
pengarang : Nicholas Leonardo
tahun terbit : 2012
Buku Ku
Judul Buku : Dokumentasi abadi
pengarang : Diandra Yusuf
tahun terbit : 2013
BUILD
SUCCESSFUL (total time: 0 seconds)