Anda di halaman 1dari 4

MODULE 6

INTERFACE

A. OBJECTIVE
Understanding interface as a provided mechanism by java, which allowing to be used by
classes.
B. BASIC THEORY
Interface is similar as class, the difference is interface keyword substituting class
keyword position. Public access determinant is used in order that interface able to be
accessed from any other classes. Suppose there is no public access determinant so classes
located in the same package can access the interface.
C. TOOLS
1. Computer.
2. NetBeans application
D. EXPERIMENT
WORK STEPS
1. Create new interface named as IntLampu.java, write down the following source code:
2.

interface IntLampu {
public static final int KEADAAN_HIDUP=1;
public static final int KEADAAN_MATI=0;

Compi
le the

public abstract void hidupkan();


public abstract void matikan();
}
program.
3. Next, create new class named as Lampu.java which is the implementation of interface
IntLampu.java.
4. Write down the following source code:

public class Lampu implements IntLampu{


private int statusLampu = 0;
public void hidupkan(){
if(this.statusLampu==KEADAAN_MATI){
this.statusLampu=KEADAAN_HIDUP;
System.out.println("Lampu Hidup");
}
else{
System.out.println("Lampu Sudah Hidup");
}
}
public void matikan(){
if(this.statusLampu==KEADAAN_HIDUP){
this.statusLampu=KEADAAN_MATI;
System.out.println("Lampu Mati");
}
else{
System.out.println("Lampu Sudah Mati");
}
}
}
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas(){
System.out.println("ada kompasnya? "+compas);
}
}
5. Compile analyze and explain the class above.
6. Create a new class with main function as implementation of TesInterface.java class, based
on this following code:
public class RoadBicycle extends Bicycle {
int diskBreak = 2;
public static void main (String []args){
RoadBicycle roadBike = new RoadBicycle();
roadBike.speedUp(100);
roadBike.changeGear(6);
roadBike.changeCandance(9);
roadBike.printStates();
roadBike.diskBreak();
}
void diskBreak(){
System.out.println("disk break = "+diskBreak);
}
}
7. Compile and run the program.
EXERCISES
1. Modify IntLampu.java interface above by changing the final variables into

Public static final int KEADAAN_HIDUP=2;


Public static final int KEADAAN_REDUP=1;
Public static final int KEADAAN_MATI=0;
And add redupkan() method
Public abstract void redupkan()
2. Modify TesInterface.java class so when it is running produce output as below:

Solution:
public class TesInterface {
public static void main (String[]arg){
Lampu lampuKamar=new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
lampuKamar.hidupkan();
lampuKamar.redupkan();
lampuKamar.redupkan();
}
}
3. Create analyze result.
E. ANALYSIS
In Lampu.java class as implements of IntLampu.java interface there is a variable
(statusLampu) with 0 as default value and two method, those are hidupkan() and matikan().
The hidupkan() method having function to turn on the light, the instruction is to change the
value of statusLampu as the value of KEADAAN_HIDUP or 1 as in intLampu.java
interface if the current value of statusLampu is 0 and to give notification that the lamp has
turned on. And giving notification that the lamp has been turned on already if the statusLampu
current value is 1, and in reverse for matikan() method.
For the exercise it is not so different with the experiment, the differences are that we add a
variable in the IntLampu.java interface (KEADAAN_REDUP) and a method (redupkan()). The

main idea of redupkan() method is similar with two other method, if we call this method while
the lamp is turned on it will reduce the lamp brightness and if we try to reduce the brightness
while the lamp brightness is reduced already it will gives notification if so and nothing is
happened.