chapter4 polymorphism

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

Post on 12-Apr-2017

327 views

Category:

Education

0 download

Embed Size (px)

TRANSCRIPT

THIS IS YOUR PRESENTATION TITLE

Object Oriented Programming

Chapter 4: Polymorphism Prepared by: Mahmoud Rafeek Alfarra

2016

OutlinesMotivation What is Polymorphism? Abstract Classes and Methods Full Examplefinal class and method

Note: I have prepared this material Based on (Liang: Introduction to Programming using Java, 10th edition, 2015)

Lecture Lets think on Polymorphism1

Suppose you will define classes to model circles, rectangles, and triangles.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? 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.Polymorphism means that a variable of a supertype can refer to a subtype object.

ExampleShape

Specialization+

3-D2-D

getArea() !!

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

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 inDeclaring abstract classes

public abstract void draw();

Declaring abstract classesEach 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/[email protected]

!

Lecture Lets focus on Implementation of Polymorphism2

Full Example

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 SalariedEmployeeSalariedEmployee 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 CommissionEmployeeCommissionEmployee 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 BasePlusCommissionEmployeeBasePlusCommissionEmployee Class

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

PracticesPractice 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/[email protected]

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.

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

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

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