MODUL 4
INHERITANCE
A. Objective
To understand
how was inheritance run.
B. Basic Theory
Inheritance
Inheritance is a
main characteristic of PBO and make progammer to be easy to manage the program.
As in a real life, kid characteristic is same as her parent. A class has been
inherited from its parent class will heritance all of the variable and function
of its parent class.
C. Tools and Materials
·
Laptop
·
NetBeans
Software
·
Java
Development Kit (JDK)
D. Steps
First
Experiment
Make a new class
named “MountainBike.java”, in this class added a special function that only
owned by MountainBike.
1. Change the Aksesor method in “Bicycle.java” class to protected
Example:
protected void speedUp () {
}
2. Write down the program code as below
package modul1;
/**
*
* @author DESY
*/
public class MountainBike extends Bicycle {
static boolean compas = true;
public static void main (String[]args){
MountainBike mountBike = new MountainBike();
mountBike.speedUp(100);
mountBike.changeGear(6);
mountBike.changeCadence(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("ada kompasnya ?? "+compas);
}
}
3.
Save file as
the name of the class
4.
Run and analyze
the result
Exercises
1.
Make a
RoadBicycle.java class that have special variable diskBreak.
Static int
diskBreak = 2;
package modul1;
/**
*
* @author DESY
*/
public class RoadBicycle
extends Bicycle {
static int diskBreak
= 2;
public static void main (String[]args){
RoadBicycle roadBike = new RoadBicycle();
roadBike.speedUp(200);
roadBike.changeGear(4);
roadBike.changeCadence(7);
roadBike.printStates();
roadBike.lookDiskBreak();
}
void lookDiskBreak
(){
System.out.println("ada diskBreaknya ?? "+diskBreak);
}
}
2.
Make a
TandemBike.java class that have special variable sadel.
Static int
sadel = 2;
package modul1;
/**
*
* @author DESY
*/
public class TandemBike extends Bicycle {
static int sadel = 2;
public static void main (String []args){
TandemBike tandembike = new TandemBike();
tandembike.speedUp(100);
tandembike.changeGear(6);
tandembike.changeCadence(9);
tandembike.printStates();
tandembike.lookSadel();
}
void lookSadel (){
System.out.println("ada sadelnya ?? "+sadel);
}
}
3.
Run and analyze
the result
run:
Speed = 200,
Gear = 4, Cadence = 7
ada diskBreaknya
?? 2
BUILD
SUCCESSFUL (total time: 0 seconds)
run:
Speed = 100,
Gear = 6, Cadence = 9
ada sadelnya ??
2
BUILD
SUCCESSFUL (total time: 0 seconds)
Calling
Constructor super class
In
this experiment we will try to access the constructor in the super class to
used by subclass.
1.
Make “Orang.java” class write down the program code as
below
/**
*
* @author DESY
*/
public class Orang {
private String nama;
private int usia;
//private String konsentrasiJurusan;
public Orang(String nama, int usia){
this.nama = nama;
this.usia = usia;
//this.konsentrasiJurusan =
konsentrasiJurusan;
}
//method
public void info(){
System.out.println("Nama : "+nama);
System.out.println("usia : "+usia);
//System.out.println("Konsentrasi
Jurusan : "+konsentrasiJurusan);
}
}
2. Next, make a new class named “Pegawai.java” a subclass of
“Orang.java”. write down the program code as below
/**
*
* @author DESY
*/
public class Pegawai extends Orang{
protected String noPegawai;
public Pegawai(String noPegawai, String
nama, int usia){
super(nama, usia);
this.noPegawai = noPegawai;
}
//method
public void info(){
System.out.println("No. Pegawai : "+this.noPegawai);
super.info();
}
}
3. Next, make a new class with main function ( ), as below
/**
*
* @author DESY
*/
public class AksesKonstSuperClass
{
public static void main (String []args){
Pegawai karyawan = new Pegawai("1245", "Mr.Budi", 24);
karyawan.info();
}
}
4. Save file as the name of the class, run and analyze the result.
Exercises
1. Make a new class named “mahasiswaTi.java” that become an
inheritance from Orang class with shown name, nim, usia, and major
consentration.
/**
*
* @author DESY
*/
public
class mahasiswaTi extends Orang{
protected
String nimmahasiswaTi;
protected
String konsentrasiJurusan;
public
mahasiswaTi(String nama, String nimmahasiswaTi, int
usia, String konsentrasiJurusan){
super(nama,
usia);
this.nimmahasiswaTi = nimmahasiswaTi;
this.konsentrasiJurusan = konsentrasiJurusan;
}
//method
public void info(){
System.out.println("NIM Mahasiswa TI : "+this.nimmahasiswaTi);
System.out.println("Konsentrasi Jurusan : "+this.konsentrasiJurusan);
super.info();
}
}
2. Next, make a class “AksesMahasiswaTi.java” that have a main function
(), as an implementation from class “MahasiswaTi.java”
/**
*
* @author DESY
*/
public
class AksesMahasiswaTi {
public static
void main (String []args){
mahasiswaTi mahasiswa = new
mahasiswaTi("Anggi",
"L200134021",
19, "Rekayasa Perangkat
Lunak");
mahasiswa.info();
}
}
3. The result as shown below
run:
NIM Mahasiswa
TI : L200134021
Konsentrasi
Jurusan : Rekayasa Perangkat Lunak
Nama : Anggi
usia : 19
BUILD SUCCESSFUL (total time: 0 seconds)
4. Analyze the result.
Exercises
1.
Is every class
that made in java must have one constructor? Prove you answer by trying to make
a class that haven’t a constructor and try to run it. Attach the class that has
been made to the report.
Constructor is a method that run in every times
object made. One of the usage of constructor is inialization of first object
condition. If we didn’t make a constructor then the constructor will do nothing
by default.
2.
Give an
attention of source code below that still have a bugs (program fault). Try to
compile that class and look at the error message that happen to report. After that,
you need to edit this class so all of the error can be solved. Report the final
result of class that you’ve made.
/*
* 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 KucingRumahan extend Cat{
int kumis;
KucingRumahan (){
this();
}
KucingRumahan (int
kumis){
super(kumis);
this.kumis
= kumis;
this.fur
= fur ;
}
void
rawatKumis(){
kumis++;
}
}
Edit
class agar semua error dapat dihiangkan menjadi seperti dibawah ini:
/**
*
* @author DESY
*/
public
class KucingRumahan extends Cat{
int kumis;
public KucingRumahan (int kumis){
this.kumis=kumis;
}
public KucingRumahan (int
kumis, String fur){
super();
this.kumis = kumis;
this.fur = fur ;
}
void rawatKumis(){
kumis++;
}
}