Chapter4 Polymorphism

Post on 12-Apr-2017

351 views 0 download

Transcript of Chapter4 Polymorphism

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.

ExampleShape

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/mahmoudRAlfarraStaff.cst.ps/mfarraYoutube.com/mralfarra1@mralfarra

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

Lecture Let’s focus on Implementation of Polymorphism

2

Full ExampleIndirect

Concrete Subclass class

Concrete Subclass class

Abstract super class

Employee

CommissionEmployee

Base PlusCommissionEmployee

SalariedEmployee

HourlyEmployee

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

THANKS!

Any questions?You can find me at:

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