You are on page 1of 3

MODUL 5

ABSTRACT CLASS

A. OBJECTIVE
Understanding abstract definition as a mechanism that allow you to make some
method in class but not with the definition, and how to use abstract class in java.
B. BASIC TEORY
Abstract is some mechanism that allow you to make some method in class but not
with the definition. The definition class specified by each inheritance class. In this
term each inheritance of abstract class need to define methods which is included as
abstract class.
C. TOOLS
1. Laptop
2. Software NetBeans
D. EXPERIMENT
WORK STEP
1. Create new class named as Kendaraan.java, write the program as following
public abstract class Kendaraan {
protected String nama; //variable lokal
public abstract void jalankan(); //abstact method
}
2. Save file with name of the class.
3. Next make new class named as Sepeda.java, write the program as following
public class Sepeda extends Kendaraan{
public Sepeda (String nama){
this.nama = nama;
}
public void jalankan (){
System.out.println("Duduklah diatas Sadel "+this.nama+" dan kayuhlah");
}
}
4. Save file with name of the class
5. Then create new class with main method which is the class sepeda

implementation
public class TesAbstrakSepeda {

public static void main (String []args){


Sepeda sepedaku = new Sepeda("Sepeda Ontel");
sepedaku.jalankan();
}
}
6. Save file with name of the class, Ana compile
7. Watch and record the result

EXERCISES
1. Create new class named as Mobil.java, which is subclass from kendaraan and

make class TesAbstrakMobil.java as implementation from class Mobil.java


Solution:
public class Mobil extends Kendaraan {
public Mobil(String jenis){
this.nama = jenis;
}
public void jalankan(){
System.out.println("Duduk di depan Setir "+this.nama+
" dan Hidupkan mesin");
}
}
public class TesAbstrakMobil {
public static void main (String []args){
Mobil mobil = new Mobil("Mobil Tua");
mobil.jalankan();
}
}
2. Add one new method on class Mobil.java
public void bunyikanTlakson(){
}
3. The result as figure

Solution:
Complite source code
public class Mobil extends Kendaraan {
public Mobil(String jenis){
this.nama = jenis;
}
public void jalankan(){
System.out.println("Duduk di depan Setir "+this.nama+
" dan Hidupkan mesin");
}
public void bunyikanTlakson(){

System.out.println("Bunyikan Tlakson "+this.nama+


" Saat di persimpangan");
}
}
public class TesAbstrakSepeda {
public static void main (String []args){
Mobil mobil=new Mobil("Mobil Tua");
mobil.jalankan();
mobil.bunyikanTlakson();
}
}

E. ANALYSIS
There is no error in Kendaraan.java even though there is a method (jalankan())
having no definition and then the method defined in Sepeda.java as subclass of
Kendaraan.java. it shows that abstract can be created and probably it has been
known that the subclasses are fit operation with the method but one and another
subclass has different action.

You might also like