0% menganggap dokumen ini bermanfaat (0 suara)
6 tayangan31 halaman

Pemahaman Abstract Class dan Interface

Dokumen ini membahas tentang abstract class dan interface di Java. Abstract class dan interface digunakan untuk mendukung konsep pewarisan ganda melalui interface.

Diunggah oleh

Ichsan
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
6 tayangan31 halaman

Pemahaman Abstract Class dan Interface

Dokumen ini membahas tentang abstract class dan interface di Java. Abstract class dan interface digunakan untuk mendukung konsep pewarisan ganda melalui interface.

Diunggah oleh

Ichsan
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd

Abstract class & Interface

Pertemuan 5

Ghifari Munawar, [Link], M.T Jurusan Teknik Komputer dan Informatika - POLBAN
Topics

1. Abstract Class
2. Interface
3. Multiple Inheritance

14/04/2021
Ghifari Munawar, [Link], M.T 2
Jurusan Teknik Komputer dan Informatika - POLBAN
(1) Abstract Class
• Abstract class merupakan suatu class yang
tidak dapat diinstansiasikan.
• Umumnya digunakan dalam konsep
inheritance
• Sebagai contoh :
• Animal : Cat, Dog, Cow..
• Object ‘animal’ itu seperti apa ?
• Person : Manager, Employee, Executive..
• ‘Person’ itu konkritnya bagaimana ?
• Abstract class merupakan suatu solusi agar
kelas (superclass) tidak diinstasiasi.

14/04/2021
Ghifari Munawar, [Link], M.T 3
Jurusan Teknik Komputer dan Informatika - POLBAN
Karakteristik abstract class
• Mengumpulkan semua properties umum
dari kelas turunannya
• Boleh memiliki data member (instance
variables)
• Abstract class dapat menyediakan :
• Implemented method : mengimplementasikan
method secara konrit (ada body method-nya)
• Abstract method : method bersifat abstrak
(tidak ada body method), dan harus
diimplementasikan oleh kelas turunannya.

14/04/2021
Ghifari Munawar, [Link], M.T 4
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd..

• Must be defined by using the abstract


keyword
• Cannot be instantiated into objects
• Can have local declared variables
• Can have method definitions and
implementations
• Must be subclassed (extends) rather than
implemented (implements)
14/04/2021
Ghifari Munawar, [Link], M.T 5
Jurusan Teknik Komputer dan Informatika - POLBAN
Contoh kasus :

14/04/2021
Ghifari Munawar, [Link], M.T 6
Jurusan Teknik Komputer dan Informatika - POLBAN
Contoh abstract class

• public abstract class Animal {


abstract int eat();
abstract void breathe();
}
• Kelas ini tidak dapat diinstansiasikan
• Semua kelas turunan (subclass) dari
Animal harus mengimplementasikan
method eat() dan breathe()

14/04/2021
Ghifari Munawar, [Link], M.T 7
Jurusan Teknik Komputer dan Informatika - POLBAN
Contoh implementasi abstract class

14/04/2021
Ghifari Munawar, [Link], M.T 8
Jurusan Teknik Komputer dan Informatika - POLBAN
Abstract Method
• Terkadang kita ingin mendefinisikan suatu method
pada superclass yang perlu diimplementasikan pada
kelas turunannya (subclass)
• Contoh : [Link]() ?
• Solusinya : abstract method
• Abstract method hanya berisi deklarasi tanpa body
method nya :
• abstract void makeASound();
• Suatu kelas yang memiliki abstract method harus
dideklarasikan sebagai abstract class
• Suatu abstract method harus diimplementasikan oleh
kelas turunannya (subclass) agar dapat diinstansiasikan

14/04/2021
Ghifari Munawar, [Link], M.T 9
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd…

• An abstract class means the class must be


extended. An abstract method means the
method must be overridden.
• Abstract method has no body. If you declare
an abstract method, you MUST mark the
class abstract as well. You can’t have an
abstract method in a non-abstract class.
• You MUST implement all abstract method.

14/04/2021
Ghifari Munawar, [Link], M.T 10
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd..

14/04/2021
Ghifari Munawar, [Link], M.T 11
Jurusan Teknik Komputer dan Informatika - POLBAN
Contoh implementasi abstract method :

14/04/2021
Ghifari Munawar, [Link], M.T 12
Jurusan Teknik Komputer dan Informatika - POLBAN
Diskusi

1. Apa saja yang membedakan antara


abstract class dengan concrete class ?
2. Jelaskan apa pertimbangan dalam
menggunakan abstract method ?
3. Jelaskan mengapa abstract class dapat
mendukung konsep inheritance ?

14/04/2021
Ghifari Munawar, [Link], M.T 13
Jurusan Teknik Komputer dan Informatika - POLBAN
(2) Interfaces
• Java does not support multiple inheritance
• What if we want an object to be multiple things ?
• Specify the form (specification, behavior) of
something (a concept)
• Not providing an implementation
• Interface’s methods define the contract / protocol
(signature) to be respected for this concept
• Based on the “has-a” relationship
• Useful when you want to use code written by
others

14/04/2021
Ghifari Munawar, [Link], M.T 14
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd..
• A special type of class – a “pure” abstract class :
• No data (only statif or final)
• Defines a set of abstract methods (prototypes)
• Doesn’t provide the implementation for the prototypes,
only the definition (signature)
• A class can implement any number of instances
• It will respect a “contract”: implement all its
method
• NB : impelementation <> inheritance
• An Interface does not have a constructor
method
14/04/2021
Ghifari Munawar, [Link], M.T 15
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd..

• It is implemented by a class (using the


keyword implements) or extended by
another interface.
• All methods in a Java interface are abstract.

14/04/2021
Ghifari Munawar, [Link], M.T 16
Jurusan Teknik Komputer dan Informatika - POLBAN
Why Use Interface ?
• When implementing a class from an interface
we force it to implement all the abstract
methods.
• The interface forces separation of what a class can
do, to how it actually does it.
• A programmer can change how something is done
at any point, without changing the function of the
class
• This facilitates the idea of polymorphism as the
method described in the interface will be
implemented by all classes that implement the
interface.
14/04/2021
Ghifari Munawar, [Link], M.T 17
Jurusan Teknik Komputer dan Informatika - POLBAN
What an interface can do
• While a class can only inherit from a single superclass

• A class can implement from one interface

• A class can implement from more than one interface

14/04/2021
Ghifari Munawar, [Link], M.T 18
Jurusan Teknik Komputer dan Informatika - POLBAN
Interface example

14/04/2021
Ghifari Munawar, [Link], M.T 19
Jurusan Teknik Komputer dan Informatika - POLBAN
Implementing interface

14/04/2021
Ghifari Munawar, [Link], M.T 20
Jurusan Teknik Komputer dan Informatika - POLBAN
Interface vs Abstract Class
Interfaces : Abstract class :
• Specify the form of a • Incomplete class (dapat
concept : not memiliki implementasi
implementing it secara parsial)
• Cannot have data • Dapat memiliki data
members, only
constants member
• Superclass : digunakan
• Lightweight to
implement untuk membentuk hirarki
dari kelas
• Multiple
implementation • Single inheritance
• Based on “has-a” • Based on “is-a”
(composition) (inheritance)

14/04/2021
Ghifari Munawar, [Link], M.T 21
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd..
• Abstract classes often have an “Is-A”
relationship
• A Student is a Person, so Person may be better
as an Abstract Class

• Interfaces often have a “Has-A” relationship


• A vehicle has an engine, so Engine may be better
as an interface

14/04/2021
Ghifari Munawar, [Link], M.T 22
Jurusan Teknik Komputer dan Informatika - POLBAN
Diskusi

1. Apa pertimbangan dalam memilih


abstract class vs interface ?
2. Manakah yang bersifat paling abstract
diantara keduanya ?

14/04/2021
Ghifari Munawar, [Link], M.T 23
Jurusan Teknik Komputer dan Informatika - POLBAN
(3) Multiple Inheritance

• Multiple inheritance merupakan suatu


konsep dimana suatu kelas memiliki lebih
dari 1 (satu) parent / superclass.

A B

14/04/2021
Ghifari Munawar, [Link], M.T 24
Jurusan Teknik Komputer dan Informatika - POLBAN
Multiple Inheritance

• Umumnya konsep ini jarang digunakan,


karena Bahasa OO tidak mendukung
dalam penerapan ini (ex : java)
• Namun untuk mengimplementasikannya,
kita dapat menggunakan interface.

14/04/2021
Ghifari Munawar, [Link], M.T 25
Jurusan Teknik Komputer dan Informatika - POLBAN
Contoh kasus multiple inheritance :

14/04/2021
Ghifari Munawar, [Link], M.T 26
Jurusan Teknik Komputer dan Informatika - POLBAN
Contd..
• Kita dapat menggunakan interface untuk
mengimplementasikan multiple inheritance
• Robodog → mengimplementasikan multiple
inheritance
• Robodog → Inherit dari Animal (abstract class)
dan Pet (interface)
Contoh deklarasi :
public class Robodog extends Animal implements Pet
{
......
}

14/04/2021
Ghifari Munawar, [Link], M.T 27
Jurusan Teknik Komputer dan Informatika - POLBAN
Demo

• Demo contoh kasus menggunakan eclipse.

14/04/2021
Ghifari Munawar, [Link], M.T 28
Jurusan Teknik Komputer dan Informatika - POLBAN
Questions

14/04/2021
Ghifari Munawar, [Link], M.T 29
Jurusan Teknik Komputer dan Informatika - POLBAN
Conclusion :
• abstract method—a method which is declared but
not defined (it has no method body)
• abstract class—a class which either (1) contains
abstract methods, or (2) has been declared abstract
• instantiate—to create an instance (object) of a class
• interface—similar to a class, but contains only
abstract methods (and possibly constants)
• multiple inheritance — object or class (subclass) can
inherit from more than one parent object or class
(superclass)

14/04/2021
Ghifari Munawar, [Link], M.T 30
Jurusan Teknik Komputer dan Informatika - POLBAN
References :

1. [Link]
[Link]/prog2/PROG2_CM2.pdf
2. Object oriented programming and Java
2nd Edition – Chapter 6
3. HeadFirst Java 2nd Edition – Chapter 8
4. Oracle academy – Java Programming.

14/04/2021
Ghifari Munawar, [Link], M.T 31
Jurusan Teknik Komputer dan Informatika - POLBAN

Anda mungkin juga menyukai