chapter4 polymorphism

22
Object Oriented Programming Chapter 4: Polymorphism Prepared by: Mahmoud Rafeek Alfarra 2016

Upload: mahmoud-alfarra

Post on 12-Apr-2017

349 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Chapter4 Polymorphism

Object Oriented Programming

Chapter 4: Polymorphism

Prepared by: Mahmoud Rafeek Alfarra

2016

Page 2: Chapter4 Polymorphism
Page 3: Chapter4 Polymorphism

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)

Page 4: Chapter4 Polymorphism

Lecture Let’s think on Polymorphism

1

Page 5: Chapter4 Polymorphism

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

Page 6: Chapter4 Polymorphism

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.

Page 7: Chapter4 Polymorphism

ExampleShape

Specialization

+

3-D2-D

Page 8: Chapter4 Polymorphism

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

Page 9: Chapter4 Polymorphism

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."

Page 10: Chapter4 Polymorphism

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

Page 11: Chapter4 Polymorphism

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.

Page 12: Chapter4 Polymorphism

THANKS!

Any questions?You can find me at:

Fb/mahmoudRAlfarraStaff.cst.ps/mfarraYoutube.com/mralfarra1@mralfarra

Page 13: Chapter4 Polymorphism

الخير والشـر فتنة ،،، فانتبـه !

Page 14: Chapter4 Polymorphism

Lecture Let’s focus on Implementation of Polymorphism

2

Page 15: Chapter4 Polymorphism

Full ExampleIndirect

Concrete Subclass class

Concrete Subclass class

Abstract super class

Employee

CommissionEmployee

Base PlusCommissionEmployee

SalariedEmployee

HourlyEmployee

Page 16: Chapter4 Polymorphism

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(); }

Page 17: Chapter4 Polymorphism

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

Page 18: Chapter4 Polymorphism

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

Page 19: Chapter4 Polymorphism

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

Page 20: Chapter4 Polymorphism

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

Page 21: Chapter4 Polymorphism

Practices

Practice 1Using charts, What is Polymorphism ?

Practice 2Compare between the Types of Classes.

Practice 3By UML class, explain the concept of Specialization.

Practice 4Give 3 examples for using polymorphism.

Practice 5Define an abstract class.

Practice 6Explain the relationship between Inheritance and Polymorphism.

Page 22: Chapter4 Polymorphism

THANKS!

Any questions?You can find me at:

Fb/mahmoudRAlfarraStaff.cst.ps/mfarraYoutube.com/mralfarra1@mralfarra