Upload
dokhuong
View
215
Download
0
Embed Size (px)
Citation preview
Pemrograman Lanjut
PTIIK - 2014
Interface
Objectives
Interfaces
Defining an Interface
How a class implements an interface
Public interfaces
Implementing multiple interfaces
Extending an interface
2
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
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
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
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
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
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
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
Inspect the interface. Is it correct?
10
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
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
Implementing an Interface
Here is another class definition:
Any number of classes can implement the same
interfaces.
13
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
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
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
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
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
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
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
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
Interface yang mendefinisikan hubungan antara
dua object menurut urutan asli dari object.
22
Penggunaan Interface
Penggunaan
interface, kita
gunakan kata kunci
implements.
Contohnya,
23
Note
Ketika class Anda mencoba mengimplementasikan
sebuah interface, selalu pastikan bahwa Anda
mengimplementasikan semua method dari
interface, jika tidak, Anda akan menemukan
kesalahan
24
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
Hubungan dari Interface ke Class
class yang mengimplementasikan interface
class yang meng-extend satu superclass dan
mengimplementasikan sebuah interface
26
Note
Sebuah interface bukan bagian dari hirarki
pewarisan class.
Class yang tidak berhubungan dapat
mengimplementasikan interface yang sama
27
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
081 331 834 734 / 088 160 127 40