Object Oriented Programming
Chapter 4: Polymorphism
Prepared by: Mahmoud Rafeek Alfarra
2016
Outlines
◉ Motivation
◉ What is Polymorphism?
◉ Abstract Classes and Methods
◉ Full Example
◉ final class and method
Note: I have prepared this material Based on (Liang: “Introduction to Programming using Java”, 10’th edition, 2015)
Lecture
Let’s think on Polymorphism
1
o Suppose you will define classes to model circles, rectangles, and
triangles.
o These classes have many common methods, but each one of them
is implemented with different way. What is the best way to
design these classes?
The answer is to use Polymorphism.
Motivation
What is Polymorphism?
o Polymorphism comes from Greek meaning “many forms”.
o In Java, polymorphism refers to the dynamic binding mechanism
that determines which method definition will be used when a
method name has been overridden.
o Polymorphism means that a variable of a supertype can refer to a
subtype object.
Example
Shape
Specialization
+
3-D2-D
getArea() !!
GeometricObject
-color: String
-filled: boolean
-dateCreated: java.util.Date
+GeometricObject()
+GeometricObject(color: String,
filled: boolean)
+getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
+getDateCreated(): java.util.Date
+toString(): String
The color of the object (default: white).
Indicates whether the object is filled with a color (default: false).
The date when the object was created.
Creates a GeometricObject.
Creates a GeometricObject with the specified color and filled
values.
Returns the color.
Sets a new color.
Returns the filled property.
Sets a new filled property.
Returns the dateCreated.
Returns a string representation of this object.
Circle
-radius: double
+Circle()
+Circle(radius: double)
+Circle(radius: double, color: String,
filled: boolean)
+getRadius(): double
+setRadius(radius: double): void
+getArea(): double
+getPerimeter(): double
+getDiameter(): double
+printCircle(): void
Rectangle
-width: double
-height: double
+Rectangle()
+Rectangle(width: double, height: double)
+Rectangle(width: double, height: double
color: String, filled: boolean)
+getWidth(): double
+setWidth(width: double): void
+getHeight(): double
+setHeight(height: double): void
+getArea(): double
+getPerimeter(): double
Type of
Class
Concrete
classes
Abstract
classes
Classes that can be used to
instantiate objects, they
provide implementations of
every method they declare.
They are used only as superclass.
They cannot be used to instantiate
objects, because, they are
incomplete. Subclasses must
declare the "missing pieces."
o You make a class abstract by declaring it with keyword abstract.
o An abstract class normally contains one or more abstract
methods.
o An abstract method is one with keyword abstract in its
declaration, as in
Declaring abstract classes
public abstract void draw();
Declaring abstract classes
Each concrete subclass of an abstract superclass
also must provide concrete implementations of
the superclass's abstract methods.
A class that contains any abstract methods must
be declared as an abstract class even if that class
contains concrete (non-abstract) methods.
THANKS!
Any questions?
You can find me at:
Fb/mahmoudRAlfarra
Staff.cst.ps/mfarra
Youtube.com/mralfarra1
@mralfarra
‫فانتبـه‬ ،،، ‫فتنة‬ ‫والشـر‬ ‫اخلري‬!
Lecture
Let’s focus on Implementation of Polymorphism
2
Full Example
Indirect
Concrete
Subclass class
Concrete
Subclass class
Abstract super
class
Employee
Commission
Employee
BasePlus
Commission
Employee
Salaried
Employee
Hourly
Employee
Employee Classpublic 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();
}
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
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
CommissionEmployee
Class
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
BasePlusCommissionEmployee
Class
o A method that is declared final in a superclass cannot be
overridden in a subclass.
o Methods that are declared private are implicitly final, because it
is impossible to override them in a subclass.
o Methods that are declared static are also implicitly final,
because static methods cannot be overridden either.
final method
Practices
Practice 1
Using charts, What is
Polymorphism ?
Practice 2
Compare between the
Types of Classes.
Practice 3
By UML class, explain
the concept of
Specialization.
Practice 4
Give 3 examples for
using polymorphism.
Practice 5
Define an abstract
class.
Practice 6
Explain the relationship
between Inheritance
and Polymorphism.
THANKS!
Any questions?
You can find me at:
Fb/mahmoudRAlfarra
Staff.cst.ps/mfarra
Youtube.com/mralfarra1
@mralfarra

‫‫Chapter4 Polymorphism

  • 1.
    Object Oriented Programming Chapter4: Polymorphism Prepared by: Mahmoud Rafeek Alfarra 2016
  • 3.
    Outlines ◉ Motivation ◉ Whatis Polymorphism? ◉ Abstract Classes and Methods ◉ Full Example ◉ final class and method Note: I have prepared this material Based on (Liang: “Introduction to Programming using Java”, 10’th edition, 2015)
  • 4.
  • 5.
    o Suppose youwill define classes to model circles, rectangles, and triangles. o These classes have many common methods, but each one of them is implemented with different way. What is the best way to design these classes? The answer is to use Polymorphism. Motivation
  • 6.
    What is Polymorphism? oPolymorphism comes from Greek meaning “many forms”. o In Java, polymorphism refers to the dynamic binding mechanism that determines which method definition will be used when a method name has been overridden. o Polymorphism means that a variable of a supertype can refer to a subtype object.
  • 7.
  • 8.
    getArea() !! GeometricObject -color: String -filled:boolean -dateCreated: java.util.Date +GeometricObject() +GeometricObject(color: String, filled: boolean) +getColor(): String +setColor(color: String): void +isFilled(): boolean +setFilled(filled: boolean): void +getDateCreated(): java.util.Date +toString(): String The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created. Creates a GeometricObject. Creates a GeometricObject with the specified color and filled values. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the dateCreated. Returns a string representation of this object. Circle -radius: double +Circle() +Circle(radius: double) +Circle(radius: double, color: String, filled: boolean) +getRadius(): double +setRadius(radius: double): void +getArea(): double +getPerimeter(): double +getDiameter(): double +printCircle(): void Rectangle -width: double -height: double +Rectangle() +Rectangle(width: double, height: double) +Rectangle(width: double, height: double color: String, filled: boolean) +getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(height: double): void +getArea(): double +getPerimeter(): double
  • 9.
    Type of Class Concrete classes Abstract classes Classes thatcan be used to instantiate objects, they provide implementations of every method they declare. They are used only as superclass. They cannot be used to instantiate objects, because, they are incomplete. Subclasses must declare the "missing pieces."
  • 10.
    o You makea class abstract by declaring it with keyword abstract. o An abstract class normally contains one or more abstract methods. o An abstract method is one with keyword abstract in its declaration, as in Declaring abstract classes public abstract void draw();
  • 11.
    Declaring abstract classes Eachconcrete subclass of an abstract superclass also must provide concrete implementations of the superclass's abstract methods. A class that contains any abstract methods must be declared as an abstract class even if that class contains concrete (non-abstract) methods.
  • 12.
    THANKS! Any questions? You canfind me at: Fb/mahmoudRAlfarra Staff.cst.ps/mfarra Youtube.com/mralfarra1 @mralfarra
  • 13.
    ‫فانتبـه‬ ،،، ‫فتنة‬‫والشـر‬ ‫اخلري‬!
  • 14.
    Lecture Let’s focus onImplementation of Polymorphism 2
  • 15.
    Full Example Indirect Concrete Subclass class Concrete Subclassclass Abstract super class Employee Commission Employee BasePlus Commission Employee Salaried Employee Hourly Employee
  • 16.
    Employee Classpublic abstractclass 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(); }
  • 17.
    public class SalariedEmployeeextends 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 SalariedEmployee Class
  • 18.
    public class CommissionEmployeeextends 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 CommissionEmployee Class
  • 19.
    public class BasePlusCommissionEmployeeextends 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 BasePlusCommissionEmployee Class
  • 20.
    o A methodthat is declared final in a superclass cannot be overridden in a subclass. o Methods that are declared private are implicitly final, because it is impossible to override them in a subclass. o Methods that are declared static are also implicitly final, because static methods cannot be overridden either. final method
  • 21.
    Practices Practice 1 Using charts,What is Polymorphism ? Practice 2 Compare between the Types of Classes. Practice 3 By UML class, explain the concept of Specialization. Practice 4 Give 3 examples for using polymorphism. Practice 5 Define an abstract class. Practice 6 Explain the relationship between Inheritance and Polymorphism.
  • 22.
    THANKS! Any questions? You canfind me at: Fb/mahmoudRAlfarra Staff.cst.ps/mfarra Youtube.com/mralfarra1 @mralfarra