Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



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

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Object-Oriented Programming: Polymorphism
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Cpt S 122 – Data Structures Polymorphism
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 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.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
(C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
Class Relationships A class defines a type of data Composition allows an object of another class to define an attribute of a class –Employee “has a”
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
CET 3640 Instructor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
 2009 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Polymorphism Chapter 10.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
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.
Learning Objectives Relationships Among Objects in an Inheritance Hierarchy. Invoking Base-class Functions from Derived Class Objects. Aiming Derived-Class.
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Inheritance
abstract classes and casting objects
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Based on slides from Deitel & Associates, Inc.
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Object-Oriented Programming: Polymorphism
Presentation transcript:

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.

 Polymorphism  Enables you to “program in the general” rather than “program in the specific.”  Polymorphism enables you to write programs that process objects that share the same superclass as if they’re all objects of that superclass, simplifying programming.  With polymorphism systems are easily extensible. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Example: Quadrilaterals  If Rectangle is derived from Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateral.  Any operation that can be performed on a Quadrilateral can also be performed on a Rectangle.  These operations can also be performed on other Quadrilateral s, such as Square s, Parallelogram s and Trapezoid s.  Polymorphism occurs when a program invokes a method through a superclass Quadrilateral variable—at execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable. © Copyright by Pearson Education, Inc. All Rights Reserved.

 In the next example, we aim a superclass reference at a subclass object.  Invoking a method on a subclass object via a superclass reference invokes the subclass functionality  The type of the referenced object, not the type of the variable, determines which method is called  This example demonstrates that an object of a subclass can be treated as an object of its superclass, enabling various interesting manipulations.  A program can create an array of superclass variables that refer to objects of many subclass types.  Allowed because each subclass object is an object of its superclass. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A superclass object cannot be treated as a subclass object, because a superclass object is not an object of any of its subclasses.  The is-a relationship applies only up the hierarchy from a subclass to its direct (and indirect) superclasses. © Copyright by Pearson Education, Inc. All Rights Reserved.

10.3 Before

© Copyright by Pearson Education, Inc. All Rights Reserved Now With Polymorphism

 When a superclass variable contains a reference to a subclass object, and that reference is used to call a method, the subclass version of the method is called.  The Java compiler allows this “crossover” because an object of a subclass is an object of its superclass (but not vice versa).  When the compiler encounters a method call made through a variable, the compiler determines if the method can be called by checking the variable’s class type.  If that class contains the proper method declaration (or inherits one), the call is compiled.  At execution time, the type of the object to which the variable refers determines the actual method to use.  This process is called dynamic binding. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Abstract classes  Sometimes it’s useful to declare classes for which you never intend to create objects.  Used only as superclasses in inheritance hierarchies, so they are sometimes called abstract superclasses.  Cannot be used to instantiate objects—abstract classes are incomplete.  Subclasses must declare the “missing pieces” to become “concrete” classes, from which you can instantiate objects; otherwise, these subclasses too, will be abstract.  An abstract class provides a superclass from which other classes can inherit and thus share a common design. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Classes that can be used to instantiate objects are called concrete classes.  Such classes provide implementations of every method they declare (including those inherited).  Concrete classes provide the specifics that make it reasonable to instantiate objects.  Not all hierarchies contain abstract classes. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Programmers often write client code that uses only abstract superclass types to reduce client code’s dependencies on a range of subclass types.  You can write a method with a parameter of an abstract superclass type.  When called, such a method can receive an object of any concrete class that directly or indirectly extends the superclass specified as the parameter’s type.  Abstract classes sometimes constitute several levels of a hierarchy. © Copyright by Pearson Education, Inc. All Rights Reserved.

 You make a class abstract by declaring it with keyword abstract.  An abstract class contains one or more abstract methods.  An abstract method is one with keyword abstract in its declaration, as in public abstract void draw(); // abstract method  Abstract methods do not provide implementations.  A class that contains abstract methods must be an abstract class even if that class contains some concrete (non-abstract) methods.  Each concrete subclass of an abstract superclass, must provide concrete implementations of each of the superclass’s abstract methods.  Constructors and static methods cannot be declared abstract. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Use an abstract method and polymorphism to perform payroll calculations based on the type of inheritance hierarchy headed by an employee.  Enhanced employee inheritance hierarchy requirements:  Salaried employees are paid a fixed weekly salary  Hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of 40 hours  Commission employees are paid a percentage of their sales  Base-salaried commission employees receive a base salary plus a percentage of their sales.  For the current pay period employees will receive an additional 10% to their base salaries. © Copyright by Pearson Education, Inc. All Rights Reserved.

 abstract class Employee represents the general concept of an employee.  Subclasses: SalariedEmployee, CommissionEmployee, HourlyEmployee and BasePlusCommissionEmployee (an indirect subclass)  Fig shows the inheritance hierarchy for our polymorphic employee-payroll application. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Abstract superclass Employee declares the methods that are common down the hierarchy.  Each employee has a first name, a last name and a social security number defined in abstract superclass Employee. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Class Employee provides methods earnings and toString, in addition to the get and set methods that manipulate Employee ’s instance variables.  An earnings method applies to all employees, but each earnings calculation depends on the employee’s class.  An abstract method—there is not enough information to determine what amount earnings should return.  Each subclass overrides earnings with an appropriate implementation.  Iterate through the array of Employee s and call method earnings for each Employee subclass object.  Method calls processed polymorphically. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Fig creates an object of each of the four concrete.  Manipulates these objects non-polymorphically, via variables of each object’s own type, then polymorphically, using an array of Employee variables.  Finally, the program polymorphically determines and outputs the type of each object in the Employee array. © Copyright by Pearson Education, Inc. All Rights Reserved.

 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 dynamic binding.  A superclass reference can be used to invoke only methods of the superclass—the subclass method are invoked polymorphically.  Attempting to invoke a subclass-only method directly on a superclass reference is a compilation error. © Copyright by Pearson Education, Inc. All Rights Reserved.

 There are four ways to assign superclass and subclass references to variables of superclass and subclass types.  Assigning a superclass reference to a superclass variable is straightforward.  Assigning a subclass reference to a subclass variable is straightforward.  Assigning a subclass reference to a superclass variable is safe, because the subclass object is an object of its superclass.  The superclass variable can be used to refer only to superclass members.  If this code refers to subclass-only members through the superclass variable, the compiler reports errors.  Attempting to assign a superclass reference to a subclass variable is a compilation error. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Check OpenLab for new labs  Check Blackboard for new quizzes  Midterm on October 28 th (two weeks)  Official review next week  Topics in the next slides  Exam will include questions and programming problems  Absolutely no electronic devices. You use one you’ll get 0 (zero).  Study, do the quizzes, do the labs, ask questions.

 Java basics  Basic commands (i.e. if, while, switch, println, Scanner etc.)  What is the Java Virtual Machine (JVM)  Commands to compile and run an application  Java Application Programming Interface (API)  Data Types  Primitive data types  Reference data types  Initial values

 Object-Oriented programming  What is a class  What is an object  Static data and why the main method is static  What are fields  What are methods  What is a constructor  Method overloading  Encapsulation  Inheritance  Polymorphism  Abstract classes  Interfaces