- Objectives
Objectives of making this report are :
- Students are able to understand what the definition of Abstract Class in Java.
- Students are able to implement Abstract Class in this practical laboratory.
- Theoritical BasisJava provides a mecanism that make possible a method determined inside of class but it is not included the definition. This method known as Abstract Method and the class called Abstract Class. Definition of class determmined by the each inherited classes. In this case, each inherited class of Abstract Class must define methods that grouped as Abstract Method.
- Tool and Material
- Laptop
- Java Development Kit (JDK 8)
- NetBeans IDE 8.0.2
- Work Steps
- Experiment
- Creating a new classs named “Kendaraan.java” then writing the codes below :
package Percobaan1;
/**
*
* @author Jhonny
*/
public abstract class Kendaraan {
protected String nama;
public abstract void jalankan(); //method abstrack
}
/**
*
* @author Jhonny
*/
public abstract class Kendaraan {
protected String nama;
public abstract void jalankan(); //method abstrack
}
- Saving and compiling as the name of the class.
- Next, creating a new class named “Sepeda.java” , writing the program codes below :
package Percobaan1;
/**
*
* @author Jhonny
*/
public class Sepeda extends Kendaraan{
public Sepeda(String nama){
this.nama = nama;
}
public void jalankan(){
System.out.println("Duduklah di atas sadel "+ this.nama +" dan kayuhlah !");
}
}
/**
*
* @author Jhonny
*/
public class Sepeda extends Kendaraan{
public Sepeda(String nama){
this.nama = nama;
}
public void jalankan(){
System.out.println("Duduklah di atas sadel "+ this.nama +" dan kayuhlah !");
}
}
- Saving and compiling as the name of the class.
- Then, creating a new class with main( ) method that is implementation of “Sepeda” class. Writing this program codes inside of this class.
package Percobaan1;
/**
*
* @author Jhonny
*/
public class TesAbstrakSepeda {
public static void main (String []args){
Sepeda sepedaku = new Sepeda("Sepeda Ontel");
sepedaku.jalankan();
}
}
/**
*
* @author Jhonny
*/
public class TesAbstrakSepeda {
public static void main (String []args){
Sepeda sepedaku = new Sepeda("Sepeda Ontel");
sepedaku.jalankan();
}
}
- Saving and compiling as the name of the class.
- AnalysisInside class of “Kendaraan.java” contains an Abstract Method that doesn’t have instructions yet. But inside of the subclass we have made, it is written the Abstract Method with instructions. Well, it works very good and get the result. An Abstract Class known that any subclasses are operations that match with the method but among one class and other classes have different treatments.
- Tasks
- Creating a new class named “Mobil.java” that is subclass of “Kendaraan” class and creating a new class named “TesAbstractMobil.java” as implementation of “Mobil.java”.
package Percobaan1;
/**
*
* @author Jhonny
*/
public class Mobil extends Kendaraan {
public Mobil(String nama){
this.nama = nama;
}
public void jalankan(){
System.out.println("Duduklah di depan dan setirlah " + this.nama + " dan Hidupkan Mesin !");
}
/**
*
* @author Jhonny
*/
public class Mobil extends Kendaraan {
public Mobil(String nama){
this.nama = nama;
}
public void jalankan(){
System.out.println("Duduklah di depan dan setirlah " + this.nama + " dan Hidupkan Mesin !");
}
package Percobaan1;
/**
*
* @author Jhonny
*/
public class TesAbstrakMobil {
public static void main (String []args){
Mobil mobilku = new Mobil("Mobil Tua");
mobilku.jalankan();
mobilku.bunyikanTlakson();
}
}
/**
*
* @author Jhonny
*/
public class TesAbstrakMobil {
public static void main (String []args){
Mobil mobilku = new Mobil("Mobil Tua");
mobilku.jalankan();
mobilku.bunyikanTlakson();
}
}
- Following the steps above.
- Adding a new method inside class of “Mobil.java”.
package Percobaan1;
/**
*
* @author Jhonny
*/
public class Mobil extends Kendaraan {
public Mobil(String nama){
this.nama = nama;
}
public void jalankan(){
System.out.println("Duduklah di depan dan setirlah " + this.nama + " dan Hidupkan Mesin !");
}
public void bunyikanTlakson(){
System.out.println("Bunyikan Tlakson " + this.nama + " saat di Persimpangan !");
}
}
/**
*
* @author Jhonny
*/
public class Mobil extends Kendaraan {
public Mobil(String nama){
this.nama = nama;
}
public void jalankan(){
System.out.println("Duduklah di depan dan setirlah " + this.nama + " dan Hidupkan Mesin !");
}
public void bunyikanTlakson(){
System.out.println("Bunyikan Tlakson " + this.nama + " saat di Persimpangan !");
}
}
- The result will be like this figure.
No comments:
Post a Comment