CISC6795: Spring 6795 1 Object-Oriented Programming: Polymorphism.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 10 Classes Continued
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.
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: 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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
(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.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance in the Java programming language J. W. Rider.
 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.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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 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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-Oriented Programming: Polymorphism
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Interface
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Based on slides from Deitel & Associates, Inc.
Week 6 Object-Oriented Programming (2): Polymorphism
Fundaments of Game Design
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Fundaments of Game Design
Object-Oriented Programming: Polymorphism
Presentation transcript:

CISC6795: Spring Object-Oriented Programming: Polymorphism

Outline 2 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

Is-a relationship & Polymorphic Behavior 3 A superclass reference can be aimed at a subclass object b.c. a subclass object is a superclass object as well Methods can be overriden When invoking a method from a superclass reference, the type of the actual referenced object determines which method is called Polymorphism: enables “programming in the general” Same invocation can produce “many forms” of results

Polymorphism 4 When invoking a method through a superclass reference, the correct subclass version of method is called, based on the type of the object being referenced by superclass reference Same method name and signature can cause different actions to occur, depending on the type of object on which the method is invoked Dynamic binding, also known as late binding Calls to overridden methods are resolved at execution time, based on the type of object referenced

Advantage of polymorphism: generality 5 Polymorphism enables programmers to deal in generalities Execution-time environment handle specifics Programmers command objects to behave in manners appropriate to those objects, without knowing the types of the objects (as long as the objects belong to the same inheritance hierarchy)

Advantage of polymorphism: extensibility 6 Extensibility: Software that invokes polymorphic behavior is independent of the object types to which messages are sent. Adding new classes to a system with minimal modifications New object types responding to existing method calls can be incorporated into a system without requiring modification of the base system. Only client code that instantiates new objects must be modified to accommodate new types.

Outline Typical reference assignments 7

Outline Polymorphically call basePlusCommissionEmployee ’s toString method 8

Outline 9 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

Abstract Classes 10 Abstract classes: classes that are too general to create real objects Cannot be instantiated, doing so leads to a compilation error. Used only as abstract superclasses for concrete subclasses and to declare reference variables In inheritance hierarchies, abstract superclasses often occupy top few levels Abstract class declares common attributes and behaviors of various classes in a class hierarchy. Typically contains one or more abstract methods that subclasses must override for them to be concrete. Instance variables and concrete methods of an abstract class are subject to normal rules of inheritance.

Abstract Classes and Methods 11 Use keyword abstract to declare abstract class, or method Abstract classes normally contain one or more abstract methods All concrete subclasses must override all inherited abstract methods Concrete classes: classes that can be instantiated

Creating Abstract Superclass Employee 12 abstract superclass Employee earnings is declared abstract No implementation can be given for earnings in Employee class

13

Outline Declare abstract class Employee Attributes common to all employees 14

Outline 15

Outline 16 abstract method earnings has no implementation

Outline 17 Class SalariedEmployee extends class Employee Call superclass constructor

Outline 18 Override earnings method so SalariedEmployee can be concrete Call superclass’s version of toString

Outline 19 Class HourlyEmployee extends class Employee Call superclass constructor

Outline 20 Override earnings method so HourlyEmployee can be concrete Override toString method

Outline 21

Outline 22

Outline 23

Outline 24 Class BasePlusCommissionEmployee extends class CommissionEmployee

Outline 25 Override toString method Call superclass’s toString method

Outline 26

Outline Assigning subclass objects to supercalss variables Implicitly and polymorphically call toString

Outline If the currentEmployee variable points to a BasePlusCommissionEmployee object Downcast Give BasePlusCommissionEmployee s a 10% base salary bonus Polymorphically call earnings method Call getClass and getName methods to display each Employee subclass object’s class name

Outline

30 Same results as when the employees were processed individually Base salary is increased by 10% Each employee’s type is displayed

Downcasting 31 Assigning a superclass variable to a subclass variable (without an explicit cast) is a compilation error. If at execution time the reference of a subclass object has been assigned to a superclass (direct or indirect) variable, one can downcast reference back to a reference of subclass type. An object can be cast only to its own type or to the type of one of its superclasses a ClassCastException occurs, if the object referenced does not have an is-a relationship with the type specified in cast operator If unsure, use instanceof to check whether object is indeed an object of an appropriate subclass type

Operator instanceof and Downcasting 32 Downcasting Convert a reference to a superclass to a reference to a subclass Allowed only if the object has an is-a relationship with the subclass getClass method Inherited from Object Returns an object of type Class getName method of class Class Returns the class’s name

final Methods and Classes 33 final methods Cannot be overridden in a subclass private and static methods are implicitly final final methods are resolved at compile time, this is known as static binding Compilers can optimize by inlining the code final classes Cannot be extended by a subclass Attempting to declare a subclass of a final class is a compilation error. All methods in a final class are implicitly final

Performance Tip 34 The compiler can decide to inline a final method call and will do so for small, simple final methods. Inlining does not violate encapsulation or information hiding, but does improve performance because it eliminates the overhead of making a method call. In the Java API, the vast majority of classes are not declared final. This enables inheritance and polymorphism—the fundamental capabilities of object-oriented programming. However, in some cases, it is important to declare classes final —typically for security reasons.

Outline 35 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

Creating and Using Interfaces 36 Interfaces, classes declared with keyword interface Contains only constants and abstract methods All fields are implicitly public, static and final All methods are implicitly public abstract methods Typically used when disparate classes need to share common methods and constants Normally declared in their own files with the same names as the interfaces and with the.java file-name extension

Class Implement Interfaces 37 A class can implement multiple interfaces Use a comma-separated list of interface names : public class ClassName extends SuperclassName implements FirstInterface, SecondInterface, … Declare each method in the interface using same signature or the class must be declared abstract 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.

Developing a Payable Hierarchy 38 Payable interface Contains method getPaymentAmount Is implemented by the Invoice and Employee classes When declaring a method in an interface, choose a method name that describes the method’s purpose in a general manner, because the method may be implemented by a broad range of unrelated classes.

Payable interface hierarchy UML diagram. 39

Outline 40 Declare interface Payable Declare getPaymentAmount method which is implicitly public and abstract

Outline Class Invoice implements interface Payable 41

Outline 42

Outline Declare getPaymentAmount to fulfill contract with interface Payable 43

Outline Class Employee implements interface Payable 44

Outline 45

Outline getPaymentAmount method is not implemented here 46

Interface Implementation & Inheritance Hierarchy 47 Objects of any subclasses of a class that implements an interface can also be thought of as objects of the interface A reference to a subclass object can be assigned to an interface variable if its superclass implements that interface Inheritance and interfaces are similar in their implementation of the “is-a” relationship. An object of a class that implements an interface may be thought of as an object of that interface type An object of any subclasses of a class that implements an interface also can be thought of as an object of the interface type.

Outline Class SalariedEmployee extends class Employee (which implements interface Payable ) 48

Outline Declare getPaymentAmount method instead of earnings method 49

Software Engineering Observation 50 “is-a” relationship between superclasses and subclasses, and between interfaces and the classes that implement them, holds when passing an object to a method. When a method parameter receives a variable of a superclass or interface type, the method processes the object received as an argument polymorphically. Using a superclass reference, we can polymorphically invoke any method specified in the superclass declaration (and in class Object ). Using an interface reference, we can polymorphically invoke any method specified in the interface declaration (and in class Object ).

Outline Declare array of Payable variables Assigning references to Invoice objects to Payable variables Assigning references to SalariedEmployee objects to Payable variables 51

Outline Call toString and getPaymentAmount methods polymorphically

Outline 53 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface

Abstract class vs interface Both can be used to define a type that permits multiple implementations Differences Abstract classes can contain implementations for some methods, interface can not Must be a subclass of an abstract class, a class can be a subclass of a certain class, and implement any other interfaces Prefer interface to abstract classes Easy to retrofit existing classes to implement a new interface E.g., implement Comparable interface so that it can be sorted by Array class E.g., implement Serializable interface to save objects into file… E.g., Implement Runnable for multithread execution 54

Common interfaces of Java API 55

Common interfaces of Java API (2) 56

Summary 57 Polymorphism To use overridden methods to effect polymorphism How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. Abstract classes declare abstract methods to create abstract classes. Interfaces, implemented by classes to assign common functionality to possibly unrelated classes Comparison of abstract class and interface Lab 6: to be assigned