Presentation is loading. Please wait.

Presentation is loading. Please wait.

O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.

Similar presentations


Presentation on theme: "O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4."— Presentation transcript:

1 O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4

2 O O O O P P Contents What is Polymorphism? 1 Polymorphism Examples 2 Abstract Classes and Methods 3 Examples : Employee’s Types 4 final Methods and Classes 5 Creating and Using Interfaces 6 Example: Interfaces 7 Be Care 8

3 O O O O P P 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.  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.

4 O O O O P P Polymorphism Examples Shape Specialization + 3-D 2-D

5 O O O O P P Polymorphism Examples

6 O O O O P P Abstract Classes and Methods 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 superclasses. They cannot be used to instantiate objects, because, they are incomplete. Subclasses must declare the "missing pieces."

7 O O O O P P Declaring 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.

8 O O O O P P Examples : Employee’s Types Indirect Concrete Subclass class Concrete Subclass class Abstract super class Employee Commission Employee Base Plus Commission Employee Salaried Employee Hourly Employee

9 O O O O P P Example: 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(); }

10 O O O O P P Example: 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

11 O O O O P P Example: 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

12 O O O O P P Example: 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

13 O O O O P P final 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.

14 O O O O P P final Methods and Classes 

15 O O O O P P 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.

16 O O O O P P Creating 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.

17 O O O O P P Example: Payments Payable Interface Invoice Class Employee Class Salaried Employee Implemented by

18 O O O O P P Example: Payable Interface interface Payable { double getPaymentAmount(); }

19 O O O O P P Example: 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 }

20 O O O O P P Example: 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.

21 O O O O P P Example: 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(); }

22 O O O O P P Be 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.

23 O O O O P P استغفروا ربكم قال الله تعالى ذكره: وَيَا قَوْمِ اسْتَغْفِرُوا رَبَّكُمْ ثُمَّ تُوبُوا إِلَيْهِ يُرْسِلِ السَّمَاءَ عَلَيْكُمْ مِدْرَارًا وَيَزِدْكُمْ قُوَّةً إِلَى قُوَّتِكُمْ وَلَا تَتَوَلَّوْا مُجْرِمِينَ ‏ ‏ ‏ [‏هود‏:52]‏

24 O O O O P P QUESTIONS? Thank You …


Download ppt "O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4."

Similar presentations


Ads by Google