You are on page 1of 34

Pemrograman Berorientasi Objek/

Algoritma dan Pemrograman 2

Fakultas Teknologi Informasi


Program Studi Teknik Informatika

Muhammad Fathurrachman, M.Kom

This presentation is based on the book title


Java How to Program 10th Edition
Chapter 10: Object Oriented Programming : Polymorphism and Interfaces
Review

Apa yang sudah kalian pelajari minggu lalu?

Image(s):
https://www.dreamstime.com/ 2
Course Objectives

 Learn the concept of


polymorphism.
 Use Overriden Methods to
effect polymorphism.
 Distinguish between
abstract and concrete
class.
 Declare abstract methods
to create abstract classes.
Polymorphism
Concept and Example

4
Polymorphism

Polymorphism enables you to “program in the


general” rather than “program in the specific.

Pada Java : Polimorfisme dapat berbentuk Kelas


Abstract atau Interface

5
Example 1

move()
Cheetah 50 M /mnt

move()

Animal Tiger 67 M /mnt

Write program in general


move()
public void move() Panda 26 M /mnt

move()
Tortoise ? M /mnt

6
Example 2

draw()
Spaceship

Space draw()
Asteroid
Object
draw()
Mars
public void
draw()

Picture source : 123rf.com and freepik.com


7
Case Study :
Payroll System
Using
Polymorphism
8
Payroll System Using
Polymorphism
Abstract Class Concrete Class and Extends Employee

Instance variable
• Firstname
• Lastname
• SocialSecurity Number
SalariedEmployee

BasePlusCommission
Employee CommisionEmployee
Employee

Abstract and Override Method


• toString() *override HourlyEmployee
• earnings() *abstract
9
10
Abstract Class
Employee
Class Declaration Setter and Getter Method
public abstract class Employee public String getFirstName() {
return firstName;
Declation of Instance Variable }
private final String firstName; public String getLastName() {
private final String lastName; return lastName;
private final String }
socialSecurityNumber;
Override Method
Declation of Constructor public String toString(){
return
public Employee(String firstName,
String.format("%s%s/nsocial " +
String lastName,
"security
String
number: %s",getFirstName()
socialSecurityNumber){
,getLastName(),
this.firstName = firstName;
this.lastName = lastName;
getSocialSecurityNumber());
this.socialSecurityNumber =
}
socialSecurityNumber;
} Abstract Method
public abstract double
11
earnings();
Pertanyaan

Apa yang dimaksud


dengan Method Abstract

? 12
Pertanyaan

Bagaimana cara deklarasi


method abstract “draw”
dengan tipe void

? 13
Concrete Class
HourlyEmployee
Class Declaration Declation of Instance Variable
public class HourlyEmployee
extends Employee private double wage;
private double hours;
Declation of Constructor
public HourlyEmployee(String firstName,
String lastName,
Setter and Getter Method
String public void setWage(double wage)
socialSecurityNumber,
double wage,
{
double hours){ this.wage = wage;
super(firstName,lastName, }
socialSecurityNumber); public double getWage() {
this.wage = wage; return wage;
this.hours = hours;
}
}

Override Method Override Abstract Method


public String toString(){ public double earnings() {
return String.format("hourly" + " if(getHours() <= 40)
employee: " + return getWage() * getHours();
"%s%n%s: $%,.2f; %s: else
%,.2f", return 40 * getWage() + (getHours()-
super.toString(),"hourly wage", 40)* getWage()*1.5;
getWage(), "hours worked", }
getHours()); 14
}
Concrete Class :
Salaried Employee
Class Declaration Declation of Instance Variable
public class SalariedEmployee
extends Employee private double weeklySalary;

Declation of Constructor
public SalariedEmployee Setter and Getter Method
(String firtsName,
String lastName,
String socialSecurityNumber, public double getWeeklySalary() {
double weeklySalary){ return weeklySalary;
super(firtsName,lastName, }
socialSecurityNumber); public void setWeeklySalary(double
this.weeklySalary = weeklySalary; weeklySalary) {
} this.weeklySalary = weeklySalary;
}
Override Method
public String toString(){
return String.format("hourly" + " Override Abstract Method
employee: " +
"%s%n%s: $%,.2f; %s: public double earnings() {
%,.2f", return getWeeklySalary();
super.toString(),"hourly wage", }
getWage(), "hours worked",
getHours()); 15
}
Concrete Class :
CommissionEmployee
Class Declaration Declation of Instance Variable
public class CommissionEmployee
extends Employee private double grossSales;
private double commissionRate;
Declation of Constructor
public CommissionEmployee
(String firstName,
Setter and Getter Method
String lastName, public double getGrossSales(){
String socialSecurityNumber,
return grossSales;
double grossSales,
double commissionRate){ }
public void setGrossSales(double
super(firstName,lastName,socialSecurityNumb grossSales){
er); this.grossSales = grossSales;
this.grossSales = grossSales; }
this.commissionRate = commissionRate;
}
Override Method Override Abstract Method
public String toString(){
return String.format("%s: %s%n%s: public double earnings() {
$%,.2f; %s: %.2f", return getCommissionRate
"commission employee",
super.toString(),
()+getGrossSales();
"gross sales", getGrossSales(), }
"commission rate",
getCommissionRate()); 16
}
Concrete Class:
BasePlusCommissionEmployee

Class Declaration Declation of Instance Variable


public class BasePlusCommissionEmployee
extends CommissionEmployee private double baseSalary;

Declation of Constructor Setter and Getter Method


public BasePlusCommissionEmployee
(String firstName, public double getBaseSalary() {
String lastName,
return baseSalary;
String socialSecurityNumber,
double grossSales, } public void setBaseSalary
double commissionRate, (double baseSalary) {
double baseSalary) { this.baseSalary = baseSalary;
super(firstName, lastName, }
socialSecurityNumber,
grossSales,
commissionRate); Override Abstract Method
this.baseSalary = baseSalary;
}
public double earnings(){
Override Method return getBaseSalary()
public String toString(){ + super.earnings();
return String.format("%s %s; %s: $%,.2f", }
"base-salaried",
super.toString(),
"base salary", getBaseSalary());
}
17
Polymorphism Test Class

Create Object
CommissionEmployee

CommissionEmployee FirstName : Sue


commissionEmployee LastName : Jones
= new CommissionEmployee( SSN : 222-22-2222
"Sue", "Jones", "222-22- GrossRate : 1000
2222", 10000, .06); CommissionRate : 0.06

System.out.printf("%s %s:%n%n%s%n%n", Call CommissionEmployee's toString


"Call CommissionEmployee's
toString with superclass reference ", with superclass reference to
"to superclass object", superclass object: commission
commissionEmployee.toString()); employee: Sue Jones social security
number: 222-22-2222 gross sales:
10000.00 commission rate: 0.06

18
Polymorphism Test Class

Create Object BasePlusCommission


Employee FirstName : Bob
LastName : Lewis
BasePlusCommissionEmployee SSN : 333-33-3333
basePlusCommissionEmployee =
new GrossRate : 5000
BasePlusCommissionEmployee( CommissionRate : 0.04
"Bob", "Lewis", "333- baseSalary : 300
33-3333",
5000, .04, 300);

Call BasePlusCommissionEmployee's
System.out.printf("%s %s:%n%n%s%n%n", toString with subclass reference to
"Call CommissionEmployee's subclass object: base-salaried commission
toString with superclass reference ", employee: Bob Lewis
"to superclass object",
commissionEmployee.toString()); social security number: 333-33-3333
gross sales: 5000.00
commission rate: 0.04
base salary: 300.00

19
Polymorphism Test Class

Create Object BasePlusCommission FirstName : Bob


Employee LastName : Lewis
CommissionEmployee SSN : 333-33-3333
commissionEmployee2 = GrossRate : 5000
basePlusCommissionEmployee; CommissionRate : 0.04
baseSalary : 300

System.out.printf("%s %s:%n%n%s%n", Call BasePlusCommissionEmployee's toString


"Call
BasePlusCommissionEmployee's " +
with superclass reference to subclass object:
"toString with base-salaried commission employee: Bob Lewis
superclass", social security number: 333-33-3333
"reference to subclass gross sales: 5000.00
object",
commission rate: 0.04
commissionEmployee2.toString() ); base salary: 300.00

20
Pertanyaan

Apakah kelas Abstract


Superclass (e.g Employee)
dapat digunakan untuk
Initialisasi object

? 21
Creating and
Using Interface

22
Interface

• Java Interface
– Standar Interaksi dari suatu object
– Berupa method abstract (public)
– Disparate Class (No class hierarchy)

23
Example

Durability
Offense
Ability Effect
Difficulty

Standar Interaksi dari Objek


Mobile Legend

24
Case Study :
Payroll System
Using
Interface
25
Payable Hirarchy

• Interface Payable can determine payment for


Employee
• Contain abstract method

26
Interface Payable

27
Interface Payable

public interface Payable {


public abstract double
getPaymentAmount();
}

28
Pertanyaan

Apakah method abstract


pada interface boleh
memiliki parameter
?
29
Class Invoice

public class Invoice implements Payable {

public double getPaymentAmount()


{
return getQuantity() *
getPricePerItem(); // calculate total cost
}

30
Class Employee

public abstract class Employee


implements Payable

Override getPaymentAmount are


not required !

31
SalariedEmployee

public class SalariedEmployee


extends Employee {
@Override
public double getPaymentAmount() {
return getWeeklySalary();
}

This class must override getPaymentAmount from interface


Payable that is implemented by Employee

32
Pertanyaan

Perbedaan antara abstract


class dan concrete
class
? 33
An interface is often used
in place of an abstract
class when there’s no
default implementation to
inherit
34

You might also like