You are on page 1of 24

Object Oriented Programming

O Chapter 4

O Polymorphism

P
2012

Prepared & Presented by:


Mahmoud Rafeek Alfarra
O
O
O
O
PP
Contents
1 What is Polymorphism?

2 Polymorphism Examples

3 Abstract Classes and Methods

4 Examples : Employee’s Types

5 final Methods and Classes

6 Creating and Using Interfaces

7 Example: Interfaces

8 Be Care
http://mfarra.cst.ps
O
O
O
O
PP
What is Polymorphism ?

 Polymorphism comes from Greek meaning


“many forms.”
 In Java, polymorphism refers to the dynamic
binding mechanism that determines which
method definition will be used when a method
name has been overridden.
 In particular, polymorphism enables us to
write programs that process objects that
share the same superclass in a class
hierarchy as if they are all objects of the
superclass.

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

 You make a class abstract by declaring it with


keyword abstract.
 An abstract class normally contains one or more
abstract methods.
 An abstract method is one with keyword abstract in
its declaration, as in
public
public abstract
abstract void
void draw();
draw();

A class that contains any abstract methods must be declared


as an abstract class even if that class contains concrete (non-
abstract) methods.

Each concrete subclass of an abstract superclass also must


provide concrete implementations of the superclass's abstract
methods.

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

public String info() {


return "The information is: "+ getfirstName()+ getLastName()+ getID() ;
}

// abstract method overridden by subclasses


public abstract double earnings();
}

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

public String info()


{
return super.info()+ getWeeklySalary() ;
} // end method info

} // end class SalariedEmployee

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

public String info()


{
return super.info()+ getGrossSales()+ getCommissionRate() ;
} // end method info
} // end class CommissionEmployee

http://mfarra.cst.ps
O
O
O
O Example: BasePlusCommissionEmployee class
PP

public class BasePlusCommissionEmployee extends CommissionEmployee


{
private double baseSalary; // base salary per week

public BasePlusCommissionEmployee( String firstName, String lastName,


String ID, double grossSales, double commissionRate, double baseSalary)
{
super( firstName, lastName, ID, grossSales, commissionRate );
this.baseSalary = baseSalary ;
}
// Set & Get
public double earnings() {
return getBaseSalary() + super.earnings();
} // end method earnings
public String info() {
return super.info()+ getBaseSalary() ;
} // end method info

} // end class BasePlusCommissionEmployee

http://mfarra.cst.ps
O
O
O
O
PP
final Methods and Classes

 A method that is declared final in a superclass


cannot be overridden in a subclass.
 Methods that are declared private are implicitly
final, because it is impossible to override them in a
subclass.
 Methods that are declared static are also implicitly
final, because static methods cannot be overridden
either.

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

SubClass1 SubClass2 SubClass3

Public
PublicTest1(){
Test1(){ Public
PublicTest1(){
Test1(){ Public
PublicTest1(){
Test1(){
}} }} }}
Public
PublicTest2(){
Test2(){ Public
PublicTest2(){
Test2(){ Public
PublicTest2(){
Test2(){
}} }} }}

To use an interface, a concrete class must specify that it


implements the interface and must declare each method in
the interface with the signature specified in the interface
declaration.
A class that does not implement all the methods of the
interface is an abstract class and must be declared
abstract.
http://mfarra.cst.ps
O
O
O
O
PP
Creating and Using Interfaces

 An interface is a collection of method definitions


(without implementations) and constant values.
 An interface definition has two components:

• The interface
public declaration.
publicinterface
interfaceidentifier{
identifier{
void
voidmethod();
method();
• The
}} interface body.

All methods declared in an interface are implicitly


public abstract methods and all fields are implicitly
public, static and final.

Unlike classes, all interface members must be public.

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() {

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


}
}
http://mfarra.cst.ps
O
O
O
O
PP
Example: Employee Class
public abstract class Employee implements Payable {
private String firstName;
private String lastName;
private String socialSecurityNumber;
public Employee( String first, String last, String ssn ) {
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// Set & Get Methods
} // end abstract class Employee

Note: We do not implement Payable method


getPaymentAmount here so this class must be
declared abstract to avoid a compilation error.

Failing to implement any method of an interface in a concrete


class that implements the interface results in a syntax error
indicating that the class must be declared abstract.
http://mfarra.cst.ps
O
O
O
O
PP
Example: Employee Class
public class SalariedEmployee extends Employee {
private double weeklySalary;
public SalariedEmployee( String first, String last, String ssn, double salary )
{
super( first, last, ssn ); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor

// Set & Get Methods

public double getPaymentAmount()


{
return getWeeklySalary();
}
}

http://mfarra.cst.ps
O
O
O
O
PP
Be Care

Attempting to declare a subclass of a final class is a


compilation error.

Assigning a superclass variable to a subclass variable (without


an explicit cast) is a compilation error.

Attempting to instantiate an object of an abstract class is a


compilation error.

Failure to implement a superclass's abstract methods in a


subclass is a compilation error unless the subclass is also
declared abstract.
‫‪O‬‬
‫‪O‬‬
‫‪O‬‬
‫‪O‬‬
‫‪PP‬‬
‫استغفروا ربكم‬

‫قال ا تعالى ذكره‪:‬‬

‫ك مْ‬
‫غ ِف ُروا َر ّب ُ‬ ‫س َت ْ‬‫ما ْ‬ ‫َو َيا َق ْو ِ‬
‫م ُتو ُبوا إ ِ َل ْي ِه ُي ْر سِ ِ‬
‫ل‬ ‫ُث ّ‬
‫م ِم ْد َرا ًرا‬‫ك ْ‬ ‫ع َل ْي ُ‬‫ء َ‬‫ما َ‬‫س َ‬ ‫ال ّ‬
‫ك مْ‬
‫ة إِ َلى ُق ّو ِت ُ‬ ‫َو َي ِز ْد ُك مْ ُق ّو ً‬
‫ج ِر ِمي َ‬
‫ن‬ ‫َو َل َت َت َو ّل ْوا ُم ْ‬
‫] هود‪[52:‬‬

‫‪http://mfarra.cst.ps‬‬
O
O
O
O
PP
Thank You …

QUESTIONS?
http://mfarra.cst.ps

You might also like