You are on page 1of 8

MODULE 4

INHERITANCE

A. OBJECTIVE
To understand what the definition and how to use inheritance in java.
B. BASIC THEORY
Inheritance is one of main feature of OOP and it is character that simplify programmer
works for create and arrange some program. Inheritance make some character that can
apply to sub class which make subclass have same character with the parent class.
C. TOOLS
1. Computer.
2. NetBeans application
D. EXPERIMENT
1.

EXPERIMENT 1.
1.1. WORK STEPS
1.1.1.Change Accessor method in Bicycle.java class become protected
Ex. : protected void speedUp(){
}
public class Bicycle {
protected int candance;
protected int speed;
protected int gear;
protected void changeCandance (int newValue){
candance = newValue; }
protected void changeGear (int newValue){
gear = newValue; }
protected void speedUp (int increment){
speed = speed + increment; }
protected void applyBreak (int decrement){
speed = speed - decrement; }
protected void printStates(){
System.out.println("Speed = "+speed+", "+"Gear = "+gear+", "
+"Candance = "+candance); }
}

1.1.2.Save and compile the codes.


1.1.3.Write down the following program code.
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.changeCandance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas(){
System.out.println("ada kompasnya? "+compas);
}
}
1.1.4.Save same as the name of the class.
1.1.5.Run file and analyze the result.
1.2. EXERCISES
1.2.1.Create RoadBicycle.java class which have specific variable that is diskBreak.
Static int diskBreak = 2;
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);
}
}
1.2.2.Create TandemBicycle.java class which have specific variable that is sadel.
Static int sadel = 2;
public class TandemBike extends Bicycle {
int sadel = 2;
public static void main (String []args){
TandemBike tanBike = new TandemBike();
tanBike.speedUp(100);

tanBike.changeGear(6);
tanBike.changeCandance(9);
tanBike.printStates();
tanBike.sadel();
}
void sadel(){
System.out.println("sadel = "+sadel);
}
}
1.2.3.Run File and analyze the result

2.

EXPERIMENT 2. CALLING SUPER CLASS CONSTRUCTOR


2.1. WORK STEPS
2.1.1. Create Orang.java class and write down the following program code.
public class Orang {
private String nama;
private int usia;
public Orang(String nama, int usia){
this.nama = nama;
this.usia = usia;
}
//method
public void info(){
System.out.println("Nama: "+nama);
System.out.println("Usia: "+usia);
}
}
2.1.2. Next create new class named as Pegawai.java which is subclass from

Orang.java. Write down the following program code.


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();
}
}

2.1.3. Next create new main function () class, as the following program code.
public class AksesKonstSuperClass {
public static void main (String []args){
Pegawai karyawan = new Pegawai("1245", "Mr.Budi", 24);
karyawan.info();
}
}
2.1.4.Save and compile the program code, Run File and analyze the result.
2.2. EXERCISES
2.2.1.Create mahasiswaTI.java class as inheritance of Orang, showing name, student
number, ages, and course.
public class mahasiswaTi extends Orang{
protected String nim;
protected String konsentrasi;
public mahasiswaTi(String nama, String nim, int usia, String konsetrasi){
super(nama,usia);
this.nim=nim;
this.konsentrasi_jurusan=konsetrasi;
}
public void info(){
System.out.println("NIM Mahasiswa :"+nim+
"\nkonsentrasi jurusan :"+konsentrasi_jurusan);
super.info();
}
}
2.2.2.Create AksesMahasiswaTi.java having main function(), as implementation for
MahasiswaTi.java.
public class AksesMahasiswaTi {
public static void main(String[]arg){
mahasiswaTi mahasiswa=new mahasiswaTi("Anggi",
"L20008001",19,"Rekayasa Perangkat Lunak");
mahasiswa.info();
}
}
2.2.3.The result suppose to be as following picture.

E.

ANALYSIS
In first experiment we create MountainBike as the subclass of Bicycle. If we look at the source
code there are no method named as speedUp(), changeGear, changeCadance() and
printStates() in MountainBike class but there is no error and the result shows as following
picture:

it because MountainBike class as subclass can use the method of Bicycle as its superclass.
Before that, there is some modification in Bicycle class as shown as following picture:

All the variables given protected access determinator, it meant that the variables can not be
accessed from other class except the current class and its subclasses, but in this case it is pointless
because in MountainBike class we are called the method not the variable. So as long as the
methode in superclass is callable, it will work.
Here are the results for the first and second practice in first experiment, and second experiment.

First practice

Second practice

Second experiment

F. TASK
1. Is every created class in java must have a constructor? Prove your answer with trying to create
classes with no cunstructor and try to run it. Attache the classes.
Solution:
public class letter {
String letters;
void write(String value){
letters=value;
}
void print(){
System.out.println("the sentences lenght is "+letters.length()
+" characters\n"+letters.toUpperCase());
}
}
public class letterDemo {
public static void main(String []args){
letter x= new letter();
x.write("information technology ums");
x.print();
}
}
The result is below

2. Note the following source code which has bugs. Please try to compile this class and analyze
the occured error to reported. Next you need to edit the class so the error is gone.
Solution :
public class KucingRumahan extend Kucing {
int kumis; //kumis length, decrease each time the cat being care of
KucingRumahan(){
this();
}
KucingRumahan(int kumis){
super(kumis);
this.kumis = kumis;
}

void rawatKumis(){
kumis++;
}
}
From the source code we know that this class suppose to create as super class named
as Kucing first, as following source code:
public class Kucing {
protected int kumis;
public Kucing(int kumis){
this.kumis=kumis;
}
public void info(){
System.out.println("kumis length = "+kumis);
}
}
Next there are two constructors, but the first constructor is not correct because the first
constructor is pointless, so it is better not write the code and it should be as below:
public class KucingRumahan extend Kucing {
int kumis; //kumis length, decrease each time the cat being care of
KucingRumahan(int kumis){
super(kumis);
this.kumis = kumis;
}
void rawatKumis(){
kumis++;
}
}
And there is written that //kumis length, decrease each time the cat being care of so the
method rawatKumis() should be as below:
void rawatKumis(){
kumis--;
}
The last, we need main function to show the running program, we can write the
function in KucingRumah.java or create a new class to work on it, I choose to write
the main function in KucingRumah.java so I write the code in very bottom before
the last } of KucingRumah.java and it looks like below:

public class KucingRumahan extends Kucing {


protected int kumis;
KucingRumahan(int kumis){
super(kumis);
this.kumis = kumis;
}
void rawatKumis(){
kumis--;
}
public void info(){
super.info();
System.out.println("after take care of kumis length ="+kumis);
}
public static void main(String[]arg){
KucingRumahan cat= new KucingRumahan(5);
cat.rawatKumis();
cat.info();
}
}
Here is the result when the File is run

You might also like