POLIMORPHISM PEMROGRAMAN LANJUT. Dr. Eng. Herman Tolle. Sistem Informasi FILKOM UB Semester Genap 2016/2017

dokumen-dokumen yang mirip
KONSEP OOP: POLYMORPHISM

KONSEP OOP: POLYMORPHISM

INHERITANCE AND POLIMORPHISM PUTU INDAH CIPTAYANI JURUSAN SISTEM INFORMASI STMIK AMIKOM

Inheritance (Pewarisan) Pengertian dasar inheritance

Bahasa Pemrograman :: Polimorfism and Abstraction

FAKULTAS TEKNIK UNIVERSITAS NEGERI YOGYAKARTA LAB SHEET PEMROGRAMAN JAVA 2

Chapter 10 Polimorphism

Polymorphism (Polimorfisme)

Gambar 1: Hirarki class

Overriding. Subclass yang berusaha memodifikasi tingkah laku yang diwarisi dari superclass. Tujuan: subclass memiliki tingkah laku yang lebih

Overriding Overloading Polymorphism

Pewarisan, Polimorfisme, dan Interface

1.Tujuan. 2. Latar Belakang. Mendefinisikan superclasses dan subclasses Override method dari superclasses Membuat method final dan class final

KARAKTERISTIK PEMROGRAMAN BERORIENTASI OBJEK (OOP) Pemrograman berorientasi Objek mempunyai karakterisitik sebagai berikut:

3/31/2011. Tujuan. Inheritance, Polymorphism,& Interfaces. Inheritance / pewarisan. Inheritance / pewarisan. Inheritance / pewarisan

Overriding, Overloading, Polymorphism. Pertemuan 10 Pemrograman Berbasis Obyek Dr. Rodiah

Pemrograman Lanjut. Interface

CSG2H3 Object Oriented Programming. Inheritance -RSM-

Pemrograman Berorientasi. Inheritance

Konsep Dasar Pemrograman Berorientasi Objek

Pemrograman Web. Object Oriented Programming in PHP 5

9/26/2011. Tujuan. Bekerja dengan Java Class Library. Pengenalan OOP. Pengenalan OOP. Class dan Object. Enkapsulasi

Bahasa Pemrograman 2.

Pemrograman Lanjut. Instance Variable Access Modifier Set dan Get Method

Pemrograman Berorientasi. Polimorfisme dan Binding

BEKERJA DENGAN JAVA CLASS LIBRARY

Adam Hendra Brata Teknik Informatika FILKOM UB Semester Genap 2015/2016

Pertemuan 4 Halaman 1/1. Class Inheritance

OBJECT ORIENTED PROGRAMMING. Day 3 : Operator dan Assignment

Jobsheet 09. Overloading and Overriding Method

BAB 1 PEMROGRAMAN BERORIENTASI OBJEK

MODUL 3. Inheritance, Abstract Class, Interface TUJUAN PRAKTIKUM LANDASAN TEORI. A. Inheritance

Polymorphism. Materi ke-8 Pemrograman Berbasis Objek

Pemrograman Berbasis Objek Operator dan Assignment

Operator dan Assignment. Pertemuan 3 Pemrograman Berbasis Obyek

Operator dan Assignment

Overriding Overloading Polymorphism

KONSEP OOP: PEWARISAN / INHERITANCE

2015 BAHASA PEMROGRAMAN I. Pertemuan VI Inheritance (Pewarisan)

SYLLABUS DAN PENDAHULUAN. Astrid Lestari Tungadi, S.Kom., M.TI.

BAB 7 OVERLOADING DAN OVERRIDING

Polymorohism in Java. Adharul Muttaqin Teknik Elektro Universitas Brawijaya Malang

JOBSHEET POLIMORFISME

Pemrograman Lanjut Class, Instance Variable dan Method PTIIK

CSG2H3 Object Oriented Programming. Polymorphism -RSM-

Composition dan Inheritance

KONSEP OOP: PEWARISAN / INHERITANCE

Pemrograman Berorientasi. Abstract & Interface

ABSTRACTION, ENCAPSULATION, INHERITANCE & POLYMORPHISM

Pengenalan OOP. Object-Oriented programming (OOP) Menitikberatkan pada konsep object sebagai elemen dasar dari program.

Polymorphism. Oleh : Agus Priyanto, M.Kom

Employee employee=new Manager();

PEWARISAN. Disusun Oleh: Reza Budiawan. Untuk: Tim Dosen Algoritma & Pemrograman Lanjut

Bahasa Pemrograman :: Inheritance

Encapsulation (Encapsulasi) Minggu 5 Pemrograman Berorientasi Objek Alfa Faridh Suni

Inheritance dan Kata Kunci static

5. Consider the following line of code : byte ohmy What is the range of values that could be assigned to the variable ohmy?

Pemrograman Lanjut Tipe Data dan Constructor PTIIK

Like mother, like daughter... [common saying]

06/03/2018. Tidak memiliki parameter. Melakukan inisialisasi semua variabel private dengan nilai awal.

1. Keyword mana yang memiliki fungsi sama dengan keyword this dan menyediakan reference ke atribut turunan dari objek? super

E-Book PEMROGRAMAN BERORIENTASI OBJEK. Disusun Oleh: Arfian Hidayat, S.Kom

Inner Class adalah class yang berada (didefinisikan) di dalam sebuah Class

Modul ini mengenalkan Anda konsep dasar mengenai Java generic types.

IKG2I4 / Software Project I

PEMROGRAMAN JAVA. Yoannita, S.Kom. Class & Method sederhana Konsep Pemrograman Berorientasi Objek. Company LOGO

INHERITANCE. Gambar 1. Kelas Hierarki Java

Pemrograman Berorientasi. Class dan Obyek 2

Data Structures. Class 5 Pointer. Copyright 2006 by The McGraw-Hill Companies, Inc. All rights reserved.

Jelaskan tentang konsep Cohesion & Coupling!

MATERI 3a Pemrograman Berorientasi Objek (S3)

KONSEP OOP: PEWARISAN / INHERITANCE

5/23/12. Inheritance. Pengertian inheritance Deklarasi inheritance Single inheritance Multilevel inheritance Access Control super keyword

Pemrograman Berorientasi Objek / Object Oriented Programming / (OOP) Nur Hasanah, M.Cs

BAHASA PEMROGRAMAN JAVA

Praktikum 3 Konsep Class, Attribute dan Method

ENKAPSULASI Data Hiding

INHERITANCE. Pelatihan Java 2 Mei 2015 Fakultas Teknologi Informasi Program Studi Teknik Informatika

ABSTRACT & INTERFACE. Pemrograman Lanjut Herman Tolle

Modul Bahasa Pemrograman 1

Object Oriented Programming (OOP) --Polimorphisme-- Saniati STMIK Teknokrat, Bandar Lampung

Pewarisan Sifat Objek. Nur Hasanah, M.Cs

Overriding Method Overloading Method Class(iii) [Superclass and subclass] [constructor overloading] Keyword Super

LAPORAN PRAKTIKUM ALGORITMA DAN STRUKTUR DATA PENGENALAN OBJECT ORIENTED PROGRAMMING

Object Oriented Programming (OOP) --Inheritance- Saniati STMIK Teknokrat, Bandar Lampung

Membuat dan Menggunakan Class

Agi Putra Kharisma., S.T., M.T. Pengembangan Berorientasi Penggunaan Ulang

Dasar Pemrograman Java

Apa yang menjadi output potongan kode diatas? Error karena tidak ada String yang di-passing kedalam konstruktor Bapak

1/5. while and do Loops The remaining types of loops are while and do. As with for loops, while and do loops Praktikum Alpro Modul 3.

Modul Pelatihan : Pembuatan Game Animasi Menggunakan Greenfoot

PRAKTIKUM 6 EXCEPTION

Pemrograman Lanjut. Class : Deeper Look 2

KONSEP Pemrograman Berorientasi Objek KARMILASARI

Tutorial FDM Nurul Huda 2008, bundled by samsonasik. Modul Java 1

PRAKTIKUM I Class dan Obyek

1. Mana diantara pilihan-pilihan berikut ini yang merupakan penulisan loop yang benar?

PERTEMUAN 5 OOP. Objektif : Praktikan mengetahui konsep OOP. Praktikan dapat membuat program sederhana dengan konsep OOP.

2. Pilih instruksi yang dapat digunakan dalam method actionperformed untuk membedakan antara tombol buttonone dan buttontwo.

What Is a Function and Why Use One?

Transkripsi:

PEMROGRAMAN LANJUT Sistem Informasi FILKOM UB Semester Genap 2016/2017 POLIMORPHISM Dr. Eng. Herman Tolle Fakultas Ilmu Komputer, Universitas Brawijaya

Kata Kunci Polymorphism Polimorfisme Supertype, Subtype Static Polymorphism Static Binding Dynamic Polymorphism Dynamic Binding Casting

Polymorphism Polymorphism Polimorfisme Morph à berubah bentuk Kemampuan dari sebuah objek untuk berubah bentuk menyesuaikan dengan referensi objek yang ditentukan oleh program yang kita buat Static Polymorphism dan Dynamic Polymorphism

Polymorphism Polymorphism means that a variable of a supertype can refer to a subtype object. A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype. Therefore, you can say that Circle is a subtype of GeometricObject and GeometricObject is a supertype for Circle. 4

Polymorphism Polymorphism: sebuah variabel dari sebuah supertype dapat merefer ke sebuah objek subtype Sebuah kelas dapat dianggap sebagai sebuah tipe data Sebuah type yang didefinisikan oleh subclass disebut subtype, dan tipe yang didefiniskan oleh kelas induknya disebut supertype. Circle is a subtype of GeometricObject and GeometricObject is a supertype for Circle. 5

Polymorphism, Dynamic Binding and Generic Programming public class PolymorphismDemo { public static void main(string[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); public static void m(object x) { System.out.println(x.toString()); class GraduateStudent extends Student { class Student extends Person { public String tostring() { return "Student"; class Person extends Object { public String tostring() { return "Person"; Method m takes a parameter of the Object type. You can invoke it with any object. An object of a subtype can be used wherever its supertype value is required. This feature is known as polymorphism. When the method m(object x) is executed, the argument x s tostring method is invoked. x may be an instance of GraduateStudent, Student, Person, or Object. Classes GraduateStudent, Student, Person, and Object have their own implementation of the tostring method. Which implementation is used will be determined dynamically by the Java Virtual Machine at runtime. This capability is known as dynamic binding. 6

Static Polymorphism In Java, static polymorphism is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters. At compile time, Java knows which method to invoke by checking the method signatures. This is called compile time polymorphism or static binding. 7

Demo Overload class DemoOverload{ public int add(int x, int y) { //method 1 return x+y; public int add(int x, int y, int z){ //method 2 return x+y+z; public int add(double x, int y){ //method 3 return (int)x+y; public int add(int x, double y){ //method 4 return x+(int)y;

class Test { public static void main(string[] args){ DemoOverload demo=new DemoOverload(); System.out.println(demo.add(2,3)); //method 1 called System.out.println(demo.add(2,3,4)); //method 2 called System.out.println(demo.add(2,3.4)); //method 4 called System.out.println(demo.add(2.5,3)); //method 3 called Results?

Dynamic Polymorphism: Misalkan suatu subclass meng-override suatu metode dari superclass nya Anggap jika dalam program kita membuat suatu objek dari subclass dan meng-assign-kan ke referensi superclass Maka jika kita memanggil metode yg di-override dari superclass td, yang akan dipanggil (dikerjakan) adalah versi metode dari subclass-nya

class Vehicle{ public void move(){ System.out.println( Vehicles can move!! ); class MotorBike extends Vehicle{ public void move(){ System.out.println( MotorBike can move and accelerate too!! );

class Test{ public static void main(string[] args){ Vehicle vh=new MotorBike(); vh.move(); // prints MotorBike can move and accelerate too!! vh=new Vehicle(); vh.move(); // prints Vehicles can move!!

Contoh public class Bicycle { public class RoadBike extends Bicycle { public class MountainBike extends Bicycle { public class TestBikes { public static void main(string[] args){ Bicycle bike01, bike02, bike03; bike01 = new Bicycle(20, 10, 1); bike02 = new MountainBike(20, 10, 5, "Dual"); bike03 = new RoadBike(40, 20, 8, 23); bike01.printdescription(); bike02.printdescription(); bike03.printdescription();

An object in Java that passes more than one IS- A tests is polymorphic in nature Sebuah objek yang dapat diisi oleh lebih dari satu bentuk IS-A ß Polimorfism Static polymorphism in Java is achieved by method overloading Dynamic polymorphism in Java is achieved by method overriding

Contoh Class induk Person dan subclass Student, kita tambahkan subclass lain dari Person yaitu Employee. Dalam Java, kita dapat membuat referensi yang merupakan tipe dari superclass ke sebuah object dari subclass tersebut. Sebagai contohnya, public static void main(string[] args) { Person ref; Student studentobject = new Student(); Employee employeeobject = new Employee(); ref = studentobject; //Person menunjuk kepada object Student //beberapa kode di sini

Sekarang dimisalkan kita punya method getname dalam superclass Person kita, dan kita override method ini dalam kedua subclasses Student dan Employee, public class Person { public String getname(){ System.out.println( Person Name: + name); return name; public class Student extends Person { public String getname(){ System.out.println( Student Name: + name); return name; public class Employee extends Person { public String getname(){ System.out.println( Employee Name: + name); return name;

public static void main(string[] args) { Person ref; Student studentobject = new Student(); Employee employeeobject = new Employee(); ref = studentobject; //Person menunjuk kepada object Student String temp = ref.getname(); //getname dari Student class dipanggil System.out.println( temp ); ref = employeeobject; //Person menunjuk kepada object Employee String temp = ref.getname(); //getname dari Employee class dipanggil System.out.println( temp );

Ketika kita mencoba memanggil method getname dari reference Person ref, method getname dari object Student akan dipanggil. Sekarang, jika kita berikan ref ke object Employee, method getname dari Employee akan dipanggil. Kemampuan dari reference untuk mengubah sifat menurut object apa yang dijadikan acuan dinamakan polimorfisme. Polimorfisme menyediakan multiobject dari subclasses yang berbeda untuk diperlakukan sebagai object dari superclass tunggal, secara otomatis menunjuk method yang tepat untuk menggunakannya ke particular object berdasar subclass yang termasuk di dalamnya

Contoh lain yang menunjukkan properti polimorfisme adalah ketika kita mencoba melalui reference ke method. Misalkan kita punya method static printinformation yang mengakibatkan object Person sebagai reference, kita dapat mereference dari tipe Employee dan tipe Student ke method ini selama itu masih subclass dari class Person. public static main( String[] args ) { Student studentobject = new Student(); Employee employeeobject = new Employee(); printinformation( studentobject ); printinformation( employeeobject ); public static printinformation( Person p ) {....

Method Matching vs. Binding Matching a method signature and binding a method implementation are two issues. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time. A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. 20

Generic Programming public class PolymorphismDemo { public static void main(string[] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); public static void m(object x) { System.out.println(x.toString()); class GraduateStudent extends Student { class Student extends Person { public String tostring() { return "Student"; class Person extends Object { public String tostring() { return "Person"; Polymorphism allows methods to be used generically for a wide range of object arguments. This is known as generic programming. If a method s parameter type is a superclass (e.g., Object), you may pass an object to this method of any of the parameter s subclasses (e.g., Student or String). When an object (e.g., a Student object or a String object) is used in the method, the particular implementation of the method of the object that is invoked (e.g., tostring) is determined dynamically. 21

Casting Objects You have already used the casting operator to convert variables of one primitive type to another. Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. In the preceding section, the statement m(new Student()); assigns the object new Student() to a parameter of the Object type. This statement is equivalent to: Object o = new Student(); // Implicit casting m(o); The statement Object o = new Student(), known as implicit casting, is legal because an instance of Student is automatically an instance of Object. 22

Why Casting Is Necessary? Suppose you want to assign the object reference o to a variable of the Student type using the following statement: Student b = o; A compile error would occur. Why does the statement Object o = new Student() work and the statement Student b = o doesn t? This is because a Student object is always an instance of Object, but an Object is not necessarily an instance of Student. Even though you can see that o is really a Student object, the compiler is not so clever to know it. To tell the compiler that o is a Student object, use an explicit casting. The syntax is similar to the one used for casting among primitive data types. Enclose the target object type in parentheses and place it before the object to be cast, as follows: 23 Student b = (Student)o; // Explicit casting

Casting from Superclass to Subclass Explicit casting must be used when casting an object from a superclass to a subclass. This type of casting may not always succeed. Apple x = (Apple)fruit; Orange x = (Orange)fruit; 24

The instanceof Operator Use the instanceof operator to test whether an object is an instance of a class: Object myobject = new Circle();... // Some lines of code /** Perform casting if myobject is an instance of Circle */ if (myobject instanceof Circle) { System.out.println("The circle diameter is " + ((Circle)myObject).getDiameter());... 25

Summary Static Polymorphism à Method overloading Dynamic Polymorphism à Method overriding Sebuah objek (dengan tipe data) dari class induk, dapat diisi dengan objek dari class turunannya ß Polymorphism Polimorfism adalah kemampuan dari Bahasa pemrograman untuk memproses objek secara berbeda tergantung pada tipe data atau class nya More specifically, it is the ability to redefine methods for derived classes. Kemampuan untuk mendefiniskan ulang metode berdasarkan kelas turunannya