Anda di halaman 1dari 28

Week 05 Polymorphism & Interface

Genap 2015/2016

Sendy Ferdian Sujadi

Memahami konsep polymorphism (different form)


Memahami polymorphic argument
Memahami
keyword
instanceof
dan
casting
polymorphism
Memahami Interface

di

Polymorphism
Polymorphic argument
Instanceof & casting in polymorphism

Polymorphism Bentuk yang berbeda (different


form)
Polymorphism merupakan suatu feature di OOP di
mana programmer dapat memiliki objek berbeda
yang berasal dari superclass yang sama
Walaupun objek-objek ini memiliki superclass yang
sama, namun mereka dapat memiliki behavior yang
berbeda
5

public abstract class Shape{


public abstract int perimeter();
}

public class Circle extends Shape{


private int radius;
public Circle(int radius){
this.radius = radius;
}
public int perimeter(){
return (int)(radius*2*Math.PI);
}
}

public class Square extends Shape{


private int side;
public Square(int side){
this.side = side;
}
public int perimeter(){
return side*4;
}
}

Variabel terikat sesuai objek (Square = Shape) mengikat secara


dinamis
Objek tidak merubah kelas, tetapi variabel dapat secara dinamis
ditugaskan untuk berbagai jenis objek
Objek mendefinisikan perilaku/behavior (yang disebut method)
6

Superclass yang sama (seperti Shape) dan objek


yang berbeda menggunakan subclass (Circle, Square)
Perilaku(behavior) yang berbeda versi berbeda
dari method yang terikat
Dynamic Binding (dinamis & terikat) == versi dapat
di-deteksi saat aplikasi run
Method yang berhubungan dengan objek akan
diketahui ketika aplikasi run dan jenis objek
ditentukan (built-in)
7

Class Animal
Class Insect
Class Spider
Class Mamal

Class Lion
Class Dog
Class Pooch

Class GreatDane
Class Zookeeper Director class

12

Director class:
ZooKeeper.java

10

Dengan Polymorphic Arguments, programmer dapat membuat


method yang dapat menerima generic object (parent objek)
dan tetap dapat bekerja dengan baik menggunakan objekobjek dari kelas turunannya

public double countSalary(Employee p){


. . . .
}

11

Programmer dapat passing reference dari Engineer dan


Manager kepada method countSalary selama kedua class
tersebut merupakan subclass dari Employee.
Employee e = new Engineer();
Employee m = new Manager();
countSalary(e);
countSalary(m);

12

Untuk mengetahui objek yang sebenarnya, dapat digunakan


operator instanceof
public double countSalary(Employee e) {
if (e instanceof Manager) {
return e.getSalary() * 2;
} else {
return e.getSalary();
}
}

13

public double countSalary(Employee e) {


if (e instanceof Manager) {
return e.getSalary() * 2;
} else {
System.out.println(e.getDepartment()); //Error
return e.getSalary();
}
}

Supaya tidak error harus di-casting terlebih dahulu menjadi


Object Engineer, caranya :
Engineer eng = (Engineer) e;
System.out.println(eng.getDepartment());
14

polymorphism is an essential feature of OOP


to use it effectively relationships between classes must be
considered ( a "big picture" of class organisation is really
necessary)
significant effort but worthy struggle
there are several benefits of using polymorphism in programs:
faster program development
better code organisation
program extensibility
easier program maintenance
15

a common base class


methods for common behaviours
some methods to inherit, some to override

subclasses for each variation


redefine methods as appropriate
subclass instances for specific behaviour

a polymorphic variable (base class)

16

ANIMAL

DOG

CAT

17

18

Director class

19

methods getType() and getVoice() work properly for different


animals
only one definition of speak() (in the base class) !
the same definition of speak() would also work with any other
(future) subclass of Animal
20

21

Java supports single inheritance (extend)


That means a subclass in Java can have only one superclass.

However, if multiple inheritance is needed, Java provides

a solution: use an interface (implements)


Interface : template : public method tanpa implementasi
Tidak dapat memiliki atribut, tetapi dapat memiliki
konstanta (public, static, final)
The class that inherits from an interface must provide the
implementation for the methods declared in the interface
22

23

24

Untuk mendefinisikan interface, kita gunakan keyword interface, sbb:


public interface <InterfaceName> {
<dataType1> <var1> = <value>;
<dataType2> <var2> = <value>;
<ReturnType1> <methodName1> ( );
<ReturnType2> <methodName2> (<parameters>);
} // interface definition ends here.

Semua methods yang ada di dalam interface secara implisit merupakan public dan abstract.
Variabel yang ada didefinisikan di dalam interface merupakan constants (tidak dapat diubah

nilainya) dan dapat diakses oleh semua instance class yang mengimplementasikan interface tsb.
Dengan kata lain, variabel tersebut bersifat public, final, dan static.
There are a few more features of interfaces related to inheritance and implementation:
A class can extend (inherit from) another class (only one) by using the keyword extends, but
it can inherit from one or more interfaces by using the keyword implements.
Just like a class, an interface can also extend one or more interfaces by using the keyword
extends.
An interface cannot implement any interface or class.
25

26

27