Professional Documents
Culture Documents
Image(s):
https://www.dreamstime.com/ 2
Course Objectives
4
Polymorphism
5
Example 1
move()
Cheetah 50 M /mnt
move()
move()
Tortoise ? M /mnt
6
Example 2
draw()
Spaceship
Space draw()
Asteroid
Object
draw()
Mars
public void
draw()
Instance variable
• Firstname
• Lastname
• SocialSecurity Number
SalariedEmployee
BasePlusCommission
Employee CommisionEmployee
Employee
? 12
Pertanyaan
? 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;
}
}
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
Create Object
CommissionEmployee
18
Polymorphism Test Class
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
20
Pertanyaan
? 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
24
Case Study :
Payroll System
Using
Interface
25
Payable Hirarchy
26
Interface Payable
27
Interface Payable
28
Pertanyaan
30
Class Employee
31
SalariedEmployee
32
Pertanyaan