Professional Documents
Culture Documents
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); }
}
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.
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: