Professional Documents
Culture Documents
O Chapter 4
O Polymorphism
P
2012
2 Polymorphism Examples
7 Example: Interfaces
8 Be Care
http://mfarra.cst.ps
O
O
O
O
PP
What is Polymorphism ?
http://mfarra.cst.ps
O
O
O
O
PP
Polymorphism Examples
Specialization
Shape
Shape
2-D 3-D
http://mfarra.cst.ps
O
O
O
O
PP
Polymorphism Examples
http://mfarra.cst.ps
O
O
O
O
PP
Abstract Classes and Methods
Classes
Classesthat
thatcan
canbebe They
Theyare
areused
usedonly
onlyas
as
used
usedtotoinstantiate
instantiate superclasses.
superclasses.They
They
objects,
objects,they
theyprovide
provide cannot
cannotbebeused
usedtoto
implementations
implementationsofof instantiate
instantiateobjects,
objects,
every
everymethod
methodthey
they because,
because,they
theyare
are
declare.
declare. incomplete. Subclasses
incomplete. Subclasses
must
mustdeclare
declarethe
the
"missing
"missingpieces."
pieces."
http://mfarra.cst.ps
O
O
O
O
PP
Declaring abstract classes
http://mfarra.cst.ps
O
O
O
O
PP
Examples : Employee’s Types
http://mfarra.cst.ps
O
O
O
O
PP
Example: Employee class
public abstract class Employee{
private String firstName;
private String lastName;
private String ID;
public Employee( String firstName, String lastName, String ID ) {
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID; }
// set & Get methods
http://mfarra.cst.ps
O
O
O
O
PP
Example: SalariedEmployee class
public class SalariedEmployee extends Employee {
private double weeklySalary;
public SalariedEmployee( String firstName, String lastName, String ID, double
weeklySalary ) {
super( firstName, lastName, ID ); // pass to Employee constructor
this.weeklySalary = weeklySalary ;
} // end four-argument SalariedEmployee constructor
// Set & Get methods
public double earnings() {
return getWeeklySalary();
} // end method earnings
http://mfarra.cst.ps
O
O
O
O
PP
Example: SalariedEmployee class
public class CommissionEmployee extends Employee {
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
public CommissionEmployee( String firstName, String lastName, String ID,
double grossSales, double commissionRate ){
super( firstName, lastName, ID );
this.grossSales= grossSales;
this.commissionRate= commissionRate;
}
public double earnings() {
return getCommissionRate() * getGrossSales();
} // end method earnings
http://mfarra.cst.ps
O
O
O
O Example: BasePlusCommissionEmployee class
PP
http://mfarra.cst.ps
O
O
O
O
PP
final Methods and Classes
http://mfarra.cst.ps
O
O
O
O
PP
final Methods and Classes
http://mfarra.cst.ps
O
O
O
O
PP
Creating and Using Interfaces
Interface
////final
finalvariables
variables
Public
PublicTest1();
Test1();
Public
PublicTest2();
Test2();
Implemented by
Public
PublicTest1(){
Test1(){ Public
PublicTest1(){
Test1(){ Public
PublicTest1(){
Test1(){
}} }} }}
Public
PublicTest2(){
Test2(){ Public
PublicTest2(){
Test2(){ Public
PublicTest2(){
Test2(){
}} }} }}
• The interface
public declaration.
publicinterface
interfaceidentifier{
identifier{
void
voidmethod();
method();
• The
}} interface body.
http://mfarra.cst.ps
O
O
O
O
PP
Example: Payments
Implemented by
http://mfarra.cst.ps
O
O
O
O
PP
Example: Payable Interface
interface Payable {
double getPaymentAmount();
}
http://mfarra.cst.ps
O
O
O
O
PP
Example: Invoice Class
public class Invoice implements Payable {
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;
public Invoice( String part, String description, int count, double price ) {
partNumber = part;
partDescription = description;
setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store price per item
} // end four-argument Invoice constructor
// Set & Get Methods
public double getPaymentAmount() {
http://mfarra.cst.ps
O
O
O
O
PP
Be Care
ك مْ
غ ِف ُروا َر ّب ُ س َت ْما ْ َو َيا َق ْو ِ
م ُتو ُبوا إ ِ َل ْي ِه ُي ْر سِ ِ
ل ُث ّ
م ِم ْد َرا ًراك ْ ع َل ْي ُء َما َس َ ال ّ
ك مْ
ة إِ َلى ُق ّو ِت ُ َو َي ِز ْد ُك مْ ُق ّو ً
ج ِر ِمي َ
ن َو َل َت َت َو ّل ْوا ُم ْ
] هود[52:
http://mfarra.cst.ps
O
O
O
O
PP
Thank You …
QUESTIONS?
http://mfarra.cst.ps