البرمجة الهدفية بلغة جافا - تعدد الأشكال

24
O O P Polymorphism Object Oriented Programming Prepared & Presented by: Mahmoud Rafeek Alfarra 2012 Chapter 4

Transcript of البرمجة الهدفية بلغة جافا - تعدد الأشكال

Page 1: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OO

PPolymorphism

Object Oriented Programming

Prepared & Presented by: Mahmoud Rafeek Alfarra

2012

Chapter 4

Page 2: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PP

http://mfarra.cst.ps

Contents

What is Polymorphism? 1

Polymorphism Examples2

Abstract Classes and Methods3

Examples : Employee’s Types4

final Methods and Classes5

Creating and Using Interfaces6

Example: Interfaces 7

Be Care8

Page 3: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

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

In particular, polymorphism enables us to write programs that process objects that

share the same superclass in a class hierarchy as if they are all objects of the

superclass.

http://mfarra.cst.ps

Page 4: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPPolymorphism Examples

http://mfarra.cst.ps

ShapeShape

Sp

eciali zation

+

3-D2-D

Page 5: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPPolymorphism Examples

http://mfarra.cst.ps

Page 6: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPAbstract Classes and Methods

http://mfarra.cst.ps

Classes that can be used to instantiate

objects, they provide implementations of every method they

declare.

Classes that can be used to instantiate

objects, they provide implementations of every method they

declare.

They are used only as superclasses. They

cannot be used to instantiate objects, because, they are

incomplete. Subclasses must declare the "missing pieces."

They are used only as superclasses. They

cannot be used to instantiate objects, because, they are

incomplete. Subclasses must declare the "missing pieces."

Page 7: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPDeclaring abstract classes

http://mfarra.cst.ps

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 in

public abstract void draw();

public abstract void draw();

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 8: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExamples : Employee’s Types

http://mfarra.cst.ps

Page 9: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: Employee class

http://mfarra.cst.ps

public 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 10: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: SalariedEmployee class

http://mfarra.cst.ps

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

Page 11: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: SalariedEmployee class

http://mfarra.cst.ps

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

Page 12: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: BasePlusCommissionEmployee class

http://mfarra.cst.ps

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

Page 13: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPfinal Methods and Classes

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.

http://mfarra.cst.ps

Page 14: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPfinal Methods and Classes

http://mfarra.cst.ps

Page 15: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PP

Public Test1(){}Public Test2(){}

Public Test1(){}Public Test2(){}

SubClass1

Implemented by

Public Test1(){}Public Test2(){}

Public Test1(){}Public Test2(){}

SubClass2

Public Test1(){}Public Test2(){}

Public Test1(){}Public Test2(){}

SubClass3

Creating and Using Interfaces

http://mfarra.cst.ps

// final variables Public Test1();

Public Test2();

// final variables Public Test1();

Public Test2();

Interface

To use an interface, a concrete class must specify that it implements the interface and must declare each method in

the interface with the signature specified in the interface declaration.

A class that does not implement all the methods of the interface is an abstract class and must be declared

abstract.

Page 16: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPCreating and Using Interfaces

An interface is a collection of method definitions

(without implementations) and constant values.

An interface definition has two components:

• The interface declaration.

• The interface body.

http://mfarra.cst.ps

public interface identifier{ void method(); }

public interface identifier{ void method(); }

All methods declared in an interface are implicitly public abstract methods and all fields are implicitly

public, static and final.

Unlike classes, all interface members must be public.

Page 17: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: Payments

http://mfarra.cst.ps

Implemented by

Page 18: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: Payable Interface

http://mfarra.cst.ps

interface Payable {double getPaymentAmount();

}

Page 19: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: Invoice Class

http://mfarra.cst.ps

public class Invoice implements Payable {private String partNumber; private String partDescription;

private int quantity; private double pricePerItem;

public Invoice( String part, String description, int count, double price ) { partNumber = part; partDescription = description; setQuantity( count ); // validate and store quantity setPricePerItem( price ); // validate and store price per item } // end four-argument Invoice constructor

// Set & Get Methods

public double getPaymentAmount() { return getQuantity() * getPricePerItem(); // calculate total cost } }

Page 20: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: Employee Class

http://mfarra.cst.ps

public abstract class Employee implements Payable { private String firstName; private String lastName; private String socialSecurityNumber;

public Employee( String first, String last, String ssn ) { firstName = first; lastName = last; socialSecurityNumber = ssn; } // end three-argument Employee constructor

// Set & Get Methods} // end abstract class Employee

Note: We do not implement Payable method getPaymentAmount here so this class must be

declared abstract to avoid a compilation error.

Failing to implement any method of an interface in a concrete class that implements the interface results in a syntax error indicating that the class must be declared abstract.

Page 21: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPExample: Employee Class

http://mfarra.cst.ps

public class SalariedEmployee extends Employee { private double weeklySalary;

public SalariedEmployee( String first, String last, String ssn, double salary ) { super( first, last, ssn ); // pass to Employee constructor setWeeklySalary( salary ); // validate and store salary } // end four-argument SalariedEmployee constructor // Set & Get Methods public double getPaymentAmount() { return getWeeklySalary(); } }

Page 22: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPBe Care

Attempting to declare a subclass of a final class is a compilation error.

Assigning a superclass variable to a subclass variable (without an explicit cast) is a compilation error.

Attempting to instantiate an object of an abstract class is a compilation error.

Failure to implement a superclass's abstract methods in a subclass is a compilation error unless the subclass is also declared abstract.

Page 23: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PPاستغفروا ربكم

: ذكره تعالى ا قال

م ك ب ر روا ف غ ت س ا م و ق ا ي ول س ر ي ه ي ل إ بوا تو م ث

را را د م م ك ي ل ع ء ما لس ام ك ت و ق لى إ ة و ق م ك د ز ي و

ن مي ر ج م وا ل و ت ت ول: ]52هود [

http://mfarra.cst.ps

Page 24: البرمجة الهدفية بلغة جافا - تعدد الأشكال

OOOO

PP

QUESTIONS?QUESTIONS?http://mfarra.cst.ps

Thank You …Thank You …