o o p polymorphism object oriented programming prepared & presented by: dr.ismail farahat...
TRANSCRIPT
OO
PPolymorphism
Object Oriented Programming
Prepared & Presented by: dr.Ismail Farahat
Chapter 4
OOOO
PPContents
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
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.
OOOO
PPPolymorphism Examples
ShapeShape
Sp
ecialization
+
3-D2-D
OOOO
PPPolymorphism Examples
OOOO
PPAbstract Classes and Methods
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."
OOOO
PPDeclaring abstract classes
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.
OOOO
PPExamples : Employee’s Types
OOOO
PPExample: Employee class
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(); }
OOOO
PPExample: SalariedEmployee class
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
OOOO
PPExample: 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
OOOO
PPExample: BasePlusCommissionEmployee 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
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.
OOOO
PPfinal Methods and Classes
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
// 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.
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.
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.
OOOO
PPExample: Payments
Implemented by
OOOO
PPExample: Payable Interface
interface Payable {double getPaymentAmount();
}
OOOO
PPExample: Invoice Class
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 } }
OOOO
PPExample: Employee Class
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.
OOOO
PPExample: Employee Class
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(); } }
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.
OOOO
PPربكم استغفروا
قال الله تعالى ذكره:
ب0ك1م3 ث1م0 وا ر4 ر1 ت4غ3ف5 و3م5 اس3 ي4ا ق4 و4اء4 م4 ل5 الس0 س5 5ل4ي3ه5 ي1ر3 ت1وب1وا إ
ة? و0 ي4ز5د3ك1م3 ق1 ا و4 ار? د3ر4 ع4ل4ي3ك1م3 م5ا ل0و3 ت5ك1م3 و4ال4 ت4ت4و4 و0 5ل4ى ق1 إ
ر5م5ين4 م1ج3[52 ]هود:
OOOO
PP
QUESTIONS?QUESTIONS?
Thank You …Thank You …