Sie sind auf Seite 1von 7

INHERITANCE DAN ABSTRACT CLASS

The main idea of OOP which differs it from other programming paradigm is that we could create CLASS that are not so different. We express the similarity of these class as Inheritance or Polymorphisme. Inheritance allows a class to inherit some or all of their properties to the other class that inherit it. Lets say we have two classes named A and B. A inherit all of its properties to B, to do this we need to use the extends keyword when we made class B.

class A { }

class B extends A { }

Since B inherit As properties, we call B as child class and A as parent class (super), Usually, we express the above statement as diagram, as shown below :

A class could have more than one child, but only one parent. These childs would have the properties of their parent.

The diagram above shows that B, C, D inherits the properties of A, we could say that E is a child of A, even if it is an undirect inheritance.

SOAL 1 :
Suppose that we have these class : class Kendaraan { int nomorPolisi; Orang pemilik; // (anggap kelas Orang telah dibuat sebelumnya) void gantiPemilik(Orang pemilikBaru) { . . . } . . . } class Mobil extends Kendaraan { int jumlahPintu; . . . } class Truk extends Kendaraan { int jumlahRoda; . . . } class Motor extends Kendaraan { int jumlahTak; // 2-tak atau 4-tak . . . }

We declare an object named mobilku as shown below :


Mobil mobilku = new Mobil();

Heres an important part of this tutorial : A Reference Variabel of class A could also be used as a Reference Variable of a class that Inherit class A. Meaning that, if we have a reference variabel of class Kendaraan named kendaraanku, we could do this :
Kendaraan kendaraanku = mobilku;

Or this :
Kendaraan kendaraanku = new Mobil();

The statement kendaraanku instanceof Mobil would hold a true value.


1. Which IF code would be executed by the code below ? System.out.println("Data Kendaraan:"); System.out.println("Nomor polisi: " + kendaraanku.nomorPolisi); //IF #1 if (kendaraanku instanceof Mobil) System.out.println("Jenis kendaraan: Mobil"); Mobil m = (Mobil)kendaraanku; System.out.println("Jumlah pintu: " + m.jumlahPintu); } //IF #2 else if (kendaraanku instanceof Truk) { System.out.println("Jenis kendaraan: Truk"); Truk t = (Truk)kendaraanku ; System.out.println("Jumlah roda: " + t.jumlahRoda); } //IF #3 else if (kendaraanku instanceof Motor) { System.out.println("Jenis kendaraan: Motor"); Motor sm = (Motor)kendaraanku ; System.out.println("Jumlah tak: " + sm.jumlahTak); } 2. Explain why that particular IF was executed !

ABSTRACT CLASS
We have three Classes : Persegi, Lingkaran, and SegiTigaSiku . These three extends class BentukGeometris.

abstract class BentukGeometri { double sisi; void setSisi(double sisiBaru) { // metode untuk mengganti ukuran sisi bentuk geometris sisi = sisiBaru; // ganti isi sisi } abstract void cetakLuas(); // metode abstrak yang harus diimplementasikan // pada kelas turunannya } // akhir kelas BentukGeometri

Since we dont know how exactly do we calculate the Area of BentukGeometri, we decide to made hitungLuas(); as an abstract method. So that any other class that inherit this class would elaborate it later. If there is an abstract method in a class, that class is an abstract class. This doesnt work conversely. setSisi(double sisiBaru); is a method to change the length of a paricular shape. It works the same for every shape so we can implement it right away.

Soal :
1. Create BentukGeometris.java as shown above, Create Persegi.java, Lingkaran.java, and SegiTigaSiku.java, and implement their hitungLuas(); method !
class Persegi extends BentukGeometris { Persegi(double sisiBaru) { sisi = sisiBaru; } void cetakLuas(){ System.out.println(Luas Persegi : ); . . . // perintah untuk mencetak luas persegi // gunakan method System.out.println(); disini } }

class Lingkarang extends BentukGeometris { Lingkaran(double diameter) { sisi = diameter; } void cetakLuas(){ System.out.println(Luas Lingkaran : ); . . . // perintah untuk mencetak luas lingkaran // gunakan method System.out.println(); disini } }

class SegiTigaSiku extends BentukGeometris { SegiTigaSiku(double sisiBaru) { sisi = sisiBaru; } void cetakLuas(){ System.out.println(Luas Segi Tiga Siku : ); . . . // perintah untuk mencetak luas segi tiga siku // gunakan method System.out.println(); disini } }

2. Use the code below and save it as Main.java in the same directory as BentukGeometris.java , Persegi.java, Lingkaran.java, and SegiTigaSiku.java import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<BentukGeometri> list = new ArrayList<BentukGeometri>(); list.add(new Persegi(15)); list.add(new Lingkaran(15)); list.add(new SegiTigaSiku(15)); for (int i = 0; i < list.size(); i++) { BentukGeometri current = list.get(i); current.cetakLuas(); } } } a. What is the output ? b. Explain how Main.java works !

Asisten Dosen DDP Ekstensi 2010