Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Polymorphism

Nurochman
Polymorphism
• From poly (many) + morph (form)
• The ability for a method to behave differently
depending on the object it is called upon.
Polymorphism
• Overloaded methods
Same name, but different input or output, e.g.
public double distance (int x, int y);
public double distance (Point p);
• Overriden methods – 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
superclass
• Overriding methods effectively replace
superclass methods for instances of the
subclass
Feline Class
class Feline {
boolean vocal = true;
void makeNoise(){
System.out.println("Non-specific cat sound");
}
void call() {
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
implements a different version of makeNoise
• The ability to make different responses to the
same message is called polymorphism
contoh
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 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
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
• The makeNoise method of Feline is not very
useful
• Can make makeNoise an abstract method and
Feline an abstract class
• Abstract classes never have instances
Class Abtract
• Class abstract dideklarasikan dengan kata kunci
abtract ditulis sebelum kata kunci class
• Class abstract memiliki minimal satu method
abstract
• Subclass dari class abstract harus meng-
override semua method abstract
• 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, Puss, Puss……");
if (vocal) makeNoise();
}
void quiet(){
vocal = false;
}
}
Lion and Moggy
class Lion extends Feline {
//wajib mengoveride method ini
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 regina = new Lion();
regina.call();
Feline catbert = new Moggy();
catbert.call();
}
}
Contoh abstract class
public abstract class LivingThing {
public void breath() {
System.out.println("Living Thing breathing...");
}
public void eat() {
System.out.println("Living Thing eating...");
}
/**
* abstract method walk
* Kita ingin method ini di-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.
• You cannot change 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
menggunakan kata kunci implements
• Class yang mengimplementasikan interface harus meng-
override semua method yang ada dalam interface.
• Sebuah interface tidak dapat dibuat instance-nya
• Sebuah class dapat mengimplementasikan lebih dari
satu interface
Interface Moveable
interface Moveable {
public void move();
}
Class Human
public class Human extends LivingThing
implements Moveable {
public void walk() {
System.out.println("Human walks...");
}

public void move() {


System.out.println("Human moves...");
}
}
Class Animal
public class Animal extends LivingThing
implements Moveable {
public void walk() {
System.out.println(“Animal walks...");
}

public void move() {


System.out.println(“Animal moves...");
}
}
Class Test
Moveable h = new Human();
h.move();
h = new Animal();
h.move();
But…!
Human h = new Human();
h.move();
//until this is OK!
h = new Animal(); //illegal
h.move();
Implementasi interface dalam Event Handling
Pertanyaan???

You might also like