Polymorphism • From poly (many) + morph (form) • The ability for a method to behave differently differently depending on the object it is called upon.
Polymorphism • Overloaded methods Same name but different input or output e g Same name, but different input or output, e.g. public double distance (int x, int y); public double distance (Point p); • Overriden methods – methods Redefined in a subclass Redefined in a subclass with the same signature (same input, same output))
Overriding Methods • Methods in subclass may have same name, parameters and return type as methods in the parameters and return type as methods in the superclass • Overriding methods effectively replace O idi th d ff ti l l superclass methods for instances of the subclass
Feline Class class Feline { boolean vocal = true; void makeNoise(){ System.out.println("Non-specific cat sound"); } void id call() ll() { System.out.println("Puss, Puss, Puss……"); if (vocal) { makeNoise(); } } void quiet() { vocal = false; } }
Lion and Moggy class Lion extends Feline { void makeNoise(){ System.out.println("Roar! Roar!"); } } class Moggy extends Feline { void makeNoise(){ System.out.println("Meow Meow"); } }
Use of overriden methods class FelineTest { public static void main(String args[]){ Feline catwoman = new Feline(); catwoman.call(); Feline regina = new Lion(); regina.call(); Feline catbert = new Moggy(); catbert.call(); } }
Polymorphism • Feline, Lion and Moggy instances all respond to call • Each responds differently because each i l implements a different version of makeNoise t diff t i f k N i • The ability to make different responses to the y p same message is called polymorphism
contoh public class Person { public String getName() { System out println(“Person Name:” + name); System.out.println(“Person 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 main( String[] args ) { Person ref; Student studentObject = new Student(); Employee employeeObject = new Employee(); ref = studentObject; //Person menunjuk kepada // object bj S Student d 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 ); }
Abstract Methods and Classes • Instances of class Feline would never represent real world objects real world objects • The makeNoise method of Feline is not very useful f l • Can make makeNoise an abstract method and Feline an abstract class • Abstract classes never have instances Abstract classes never have instances
Class Abtract • Class abstract dideklarasikan dengan kata kunci abtract ditulis sebelum kata kunci class ditulis sebelum kata kunci class • Class abstract memiliki menimal satu method abstract bt t • Subclass dari class abstract harus meng‐ g override semua method abstract • Class abstract tidak dapat dibuat instance, yang Class abstract tidak dapat dibuat instance yang bisa hanya class concrete
Abstract Feline abstract class Feline { boolean vocal = true; abstract void makeNoise(); void call() { System.out.println("Puss, y p ( , Puss, , Puss……"); ) if (vocal) makeNoise(); } void quiet(){ vocal = false; } }
Contoh abstract class public abstract class LivingThing { public void breath() { System out println("Living Thing breathing System.out.println("Living breathing..."); "); } public void eat() p () { System.out.println("Living Thing eating..."); } /** * abstract method walk * Kita ingin method ini di di-overridden overridden oleh subclasses */ public abstract void walk(); }
public class Human extends LivingThing { public void walk() { System.out.println("Human walks..."); } }
Interface • An interface is like an abstract class, but all methods are abstract and all fields are final. • All methods must be implemented in the subclasses. b l • You cannot change g the value of an interface field.
Interface • Seperti class yang terdiri dari field dan method abstract • Semua field dalam interface adalah final (konstan) Class yang akan mengimplementasikan interface • Class yang akan mengimplementasikan interface menggunakan kata kunci implements • Class yang mengimplementasikan interface harus Class yang mengimplementasikan interface harus meng‐override semua method yang ada dalam interface. interface • Sebuah interface tidak dapat dibuat instance‐nya • Sebuah class dapat mengimplementasikan lebih dari satu interface
Interface Moveable interface Moveable { public void move(); public void move(); }
Class Human public class Human extends LivingThing implementas Moveable { public void walk() { System.out.println("Human walks..."); } public void move() { System.out.println("Human moves..."); } }
Class Animal public class Animal extends LivingThing implementas Moveable { public void walk() { System.out.println(“Animal walks..."); } public void move() { System.out.println(“Animal moves..."); } }
Class Test Moveable h = new Human(); Moveable a = new Animal(); Moveable a = new Animal(); h.move(); a.move();