Object Oriented Programming_Chapter 4 (Two Lectures)
1- Let’s think on polymorphism
2- Let’s focus on Full Example
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
3. 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)
5. 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
6. 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.
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 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."
10. 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();
11. 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.
16. 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();
}
17. 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
18. 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
19. 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
20. 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
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.