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

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 – Sections covered
Java SE 8 for Programmers, Third Edition Advanced Java Programming.
Object Oriented Programming: Inheritance Chapter 9.
Object Oriented Programming using Java - Polymorphism
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Dr. S. HAMMAMI Ahmad Al-Rjoub Chapter 4 Polymorphism CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science.
Interfaces Need arising from software engineering –Disparate groups of programmers need to agree to a “contract” that spells out how their software interacts.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
Chapter 10 Object-Oriented Programming: Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance ndex.html ndex.htmland “Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming: Inheritance Chapter 9.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Inheritance
abstract classes and casting objects
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Object Oriented Programming: Inheritance
Java Inheritance.
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Inheritance
Presentation transcript:

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

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

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.

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

O O O O P P Polymorphism Examples

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

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.

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

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(); }

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

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

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

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.

O O O O P P final Methods and Classes 

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.

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.

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

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

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 }

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.

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(); }

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.

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

O O O O P P QUESTIONS? Thank You …