29
Pemrograman Lanjut PTIIK - 2014 Interface

Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Embed Size (px)

Citation preview

Page 1: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Pemrograman Lanjut

PTIIK - 2014

Interface

Page 2: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Objectives

Interfaces

Defining an Interface

How a class implements an interface

Public interfaces

Implementing multiple interfaces

Extending an interface

2

Page 3: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Introduction

Java has single inheritance, only. This means

that a child class inherits from only one parent

class. Usually this is all you need. But sometimes

multiple inheritance would be convenient.

Interfaces give Java some of the advantages of

multiple inheritance without the disadvantages.

Interfaces are crucial to GUI programming

3

Page 4: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface

An interface describes aspects of a class other

than those that it inherits from its parent.

An interface is a set of requirements that the

class must implement.

An interface is a list of constants and method

declarations. The method declarations DO NOT

include an implementation (there is no method

body). A class that implements an interface must

implement each of the methods listed in the

interface.

4

Page 5: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface

A class can extend one parent class to inherit

the methods and instance variables of that

parent. A class can also implement an interface

to gain additional methods and constants.

However, the additional methods must be

explicitly written as part of the class definition.

The interface is a list of requirements that the

class definition must explicitly meet (through

code, not through inheritance).

5

Page 6: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface Definition

An interface definition looks like this:

A method declaration is simply an access modifier, a

return type, and a method signature followed by a

semicolon.

This looks somewhat like a class definition. But no

objects can be constructed from it.

6

Page 7: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Objects can be constructed from a class that

implements an interface.

A class implements an interface by doing this:

A class always extends just one parent but may

implement several interfaces.

7

Page 8: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Example interface

A method in an interface cannot be made private. A

method in an interface is public by default. The constants

in an interface are public static final by default.

8

Page 9: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

The second interface (default) is the preferred

way to define an interface. The defaults are

assumed and not explicitly coded.

A class that implements an interface must implement

each method in the interface.

Methods from the interface must be declared public in

the class.

Constants from the interface can be used as if they

had been defined in the class.

Constants should not be redefined in the class.

9

Page 10: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Inspect the interface. Is it correct?

10

Page 11: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Implementing an Interface

A class definition must always extend one parent, but it

can implement zero or more interfaces:

The body of the class definition is the same as always.

However, since it implements an interface the body must

have a definition of each of the methods declared in the

interface.

11

Page 12: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Implementing an Interface

The class definition can use access modifiers as usual.

Here is a class definition that implements three

interfaces:

Now BigClass must provide a method definition for every

method declared in the three interfaces.

12

Page 13: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Implementing an Interface

Here is another class definition:

Any number of classes can implement the same

interfaces.

13

Page 14: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface as a Type

An interface can be used as a data type for a reference

variable.

Since Toy and Book implement Taxable, they can both

be used with a reference variable of type Taxable:

14

Page 15: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Public Interfaces

It is OK if two interfaces ask for the same method. A

class that implements both interfaces only needs to

provide one complete method definition to satisfy both

interfaces.

An interface can be made public. In fact, this is usually

what is done.

A public interface can be implemented by any class in

any file. Many graphical user interface components

implement public interfaces. You must use them to work

with the GUI features of Java.

15

Page 16: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Hierarchy of Interfaces

An interface can be an extension of another

interface (but not an extension of a class)

A complex hierarchy of interfaces can be

constructed using this feature.

This is an advanced feature which you will

probably not need to use.

16

Page 17: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Hierarchy of Interfaces

Contohnya, misal kita punya dua interface

StudentInterface dan PersonInterface.

Jika StudentInterface meng-extend

PersonInterface, maka ia akan mewariskan

semua deklarasi method dalam PersonInterface

17

Page 18: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface vs. Abstract Class

Method interface tidak punya tubuh

Sebuah interface hanya dapat mendefinisikan

konstanta dan abstract method

Interface tidak langsung mewariskan hubungan

dengan class “istimewa” lainnya, mereka

didefinisikan secara independent.

18

Page 19: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface vs. Class

Bagaimanapun, Anda tidak dapat membuat

instance dari sebuah interface. Contohnya:

Interface maupun class dapat mendefinisikan

method.

Sebuah interface tidak punya sebuah kode

implementasi sedangkan class memiliki salah

satunya

19

Page 20: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Membuat Interface

Mari kita ambil contoh class Line dimana berisi

method yang menghitung panjang dari garis dan

method yang membandingkan object Line ke

object dari class yang sama.

Sekarang, misalkan kita punya class yang lain

yaitu MyInteger dimana berisi method yang

membandingkan object MyInteger ke object dari

class yang sama.

20

Page 21: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Membuat Interface

Kedua class tersebut mempunyai method yang mirip

yaitu membandingkan suatu object dari object lain dalam

tipe yang sama, tetapi mereka tidak berhubungan sama

sekali.

Supaya dapat menjalankan cara untuk memastikan

bahwa dua class tersebut mengimplementasikan

beberapa method dengan tanda yang sama, kita dapat

menggunakan sebuah interface.

Misal : interface Relation yang mempunyai deklarasi

method pembanding.

21

Page 22: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Interface yang mendefinisikan hubungan antara

dua object menurut urutan asli dari object.

22

Page 23: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Penggunaan Interface

Penggunaan

interface, kita

gunakan kata kunci

implements.

Contohnya,

23

Page 24: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Note

Ketika class Anda mencoba mengimplementasikan

sebuah interface, selalu pastikan bahwa Anda

mengimplementasikan semua method dari

interface, jika tidak, Anda akan menemukan

kesalahan

24

Page 25: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Hubungan dari Interface ke Class

Class dapat mengimplementasikan sebuah

interface selama kode implementasi untuk

semua method yang didefinisikan dalam

interface tersedia

Class hanya dapat meng-EXTEND SATU

superclass, tetapi dapat meng-IMPLEMENTASI-

kan BANYAK interface

25

Page 26: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Hubungan dari Interface ke Class

class yang mengimplementasikan interface

class yang meng-extend satu superclass dan

mengimplementasikan sebuah interface

26

Page 27: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Note

Sebuah interface bukan bagian dari hirarki

pewarisan class.

Class yang tidak berhubungan dapat

mengimplementasikan interface yang sama

27

Page 28: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

Latihan

Buat interface MakhlukHidup

Makan

Berjalan

Bersuara

Buat interface Identitas

TampilkanNama

TampilkanUmur

Buat class Manusia dan Hewan

Manusia mengimplementasikan interface

MakhlukHidup dan Identitas

Hewan hanya mengimplementasikan MakhlukHidup

28

Page 29: Pemrograman Lanjut - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/05/Interface.pdf · Interface vs. Abstract Class Method interface tidak punya tubuh Sebuah interface hanya

[email protected]

081 331 834 734 / 088 160 127 40