Polymorphism, Interfaces & Operator Overloading

Slides:



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

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
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
Object Oriented Programming using Java - Polymorphism
 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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Object Oriented Software Development
 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 Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
 2009 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Inheritance ndex.html ndex.htmland “Java.
 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.
Object-Oriented Design
Object-Oriented Programming: Polymorphism
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
OOP What is problem? Solution? OOP
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Introduction to Classes and Objects
Lecture 23 Polymorphism Richard Gesick.
Object-Oriented Programming: Interface
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Introduction to Classes and Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Polymorphism and Type Casting
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Abstract Classes and Interfaces
Fundaments of Game Design
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

Polymorphism, Interfaces & Operator Overloading 11 Polymorphism, Interfaces & Operator Overloading

OBJECTIVES In this chapter you will learn: The concept of polymorphism and how it enables you to "program in the general." To use overridden methods to effect polymorphism. To distinguish between abstract and concrete classes. To declare abstract methods to create abstract classes. How polymorphism makes systems extensible and maintainable. To determine an object's type at execution time. To create sealed methods and classes. To declare and implement interfaces. To overload operators to enable them to manipulate objects.

11.1 Introduction 11.2 Polymorphism Examples 11.3 Demonstrating Polymorphic Behavior 11.4 Abstract Classes and Methods 11.5 Case Study: Payroll System Using Polymorphism 11.5.1 Creating Abstract Base Class Employee 11.5.2 Creating Concrete Derived Class SalariedEmployee 11.5.3 Creating Concrete Derived Class HourlyEmployee 11.5.4 Creating Concrete Derived Class CommissionEmployee 11.5.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 11.5.6 Polymorphic Processing, Operator is and Downcasting 11.5.7 Summary of the Allowed Assignments Between Base Class and Derived Class Variables

11.6   sealed Methods and Classes 11.7   Case Study: Creating and Using Interfaces 11.7.1  Developing an IPayable Hierarchy 11.7.2  Declaring Interface IPayable 11.7.3  Creating Class Invoice 11.7.4  Modifying Class Employee to Implement Interface IPayable 11.7.5  Modifying Class SalariedEmployee for Use in the IPayable Hierarchy 11.7.6  Using Interface IPayable to Process Invoices and Employees Polymorphically 11.7.7  Common Interfaces of the .NET Framework Class Library 11.8   Operator Overloading 11.9   (Optional) Software Engineering Case Study: Incorporating Inheritance and Polymorphism into the ATM System 11.10   Wrap-Up

Software Engineering Observation 11.1 Polymorphism promotes extensibility: Software that invokes polymorphic behavior is independent of the object types to which messages are sent. New object types that can respond 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 PolymorphismTest .cs (1 of 3)

Outline PolymorphismTest .cs (2 of 3)

Outline PolymorphismTest .cs (3 of 3)

Software Engineering Observation 11.2 An abstract class declares common attributes and behaviors of the various classes that inherit from it, either directly or indirectly, in a class hierarchy. An abstract class typically contains one or more abstract methods or properties that concrete derived classes must override. The instance variables, concrete methods and concrete properties of an abstract class are subject to the normal rules of inheritance.

Common Programming Error 11.1 Attempting to instantiate an object of an abstract class is a compilation error.

Common Programming Error 11.2 Failure to implement a base class’s abstract methods and properties in a derived class is a compilation error unless the derived class is also declared abstract.

Software Engineering Observation 11.3 A derived class can inherit “interface” or “implementation” from a base class. Hierarchies designed for implementation inheritance tend to have their functionality high in the hierarchy—each new derived class inherits one or more methods that were implemented in a base class, and the derived class uses the base class implementations. Hierarchies designed for interface inheritance tend to have their functionality lower in the hierarchy—a base class specifies one or more abstract methods that must be declared for each concrete class in the hierarchy, and the individual derived classes override these methods to provide derived-class-specific implementations.

Fig. 11.2 | Employee hierarchy UML class diagram.

Fig. 11.3 | Polymorphic interface for the Employee hierarchy classes.

Outline Employee.cs (1 of 2)

Outline Employee.cs (2 of 2)

Outline SalariedEmployee .cs (1 of 2)

Outline SalariedEmployee .cs (2 of 2)

Outline HourlyEmployee.cs (1 of 3)

Outline HourlyEmployee.cs (2 of 3)

Outline HourlyEmployee.cs (3 of 3)

Outline CommissionEmployee .cs (1 of 2)

Outline CommissionEmployee .cs (2 of 2)

Outline BasePlusCommission Employee.cs (1 of 2)

Outline BasePlusCommission Employee.cs (2 of 2)

Outline PayrollSystemTest. cs (1 of 5)

Outline PayrollSystemTest. cs (2 of 5)

Outline PayrollSystemTest. cs (3 of 5)

Outline PayrollSystemTest. cs (4 of 5)

Outline PayrollSystemTest. cs (5 of 5)

Common Programming Error 11.3 Assigning a base class variable to a derived class variable (without an explicit downcast) is a compilation error.

Software Engineering Observation 11.4 If at execution time the reference to a derived class object has been assigned to a variable of one of its direct or indirect base classes, it is acceptable to cast the reference stored in that base class variable back to a reference of the derived class type. Before performing such a cast, use the is operator to ensure that the object is indeed an object of an appropriate derived class type.

Common Programming Error 11.4 When downcasting an object, an InvalidCastException (of namespace System) occurs if at execution time the object does not have an is-a relationship with the type specified in the cast operator. An object can be cast only to its own type or to the type of one of its base classes.

Performance Tip 11.1 The compiler can decide to inline a sealed method call and will do so for small, simple sealed methods. Inlining does not violate encapsulation or information hiding, but does improve performance because it eliminates the overhead of making a method call.

Common Programming Error 11.5 Attempting to declare a derived class of a sealed class is a compilation error.

Software Engineering Observation 11.5 In the FCL, the vast majority of classes are not declared sealed. This enables inheritance and polymorphism—the fundamental capabilities of object-oriented programming.

Common Programming Error 11.6 It is a compilation error to declare an interface member public or abstract explicitly, because they are redundant in interface member declarations. It is also a compilation error to specify any implementation details, such as concrete method declarations, in an interface.

Common Programming Error 11.7 Failing to declare any member of an interface in a class that implements the interface results in a compilation error.

Good Programming Practice 11.1 By convention, the name of an interface begins with "I". This helps distinguish interfaces from classes, improving code readability.

Good Programming Practice 11.2 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.

Fig. 11.10 | IPayable interface and class hierarchy UML class diagram.

Outline IPayable.cs

Outline Invoice.cs (1 of 4)

Outline Invoice.cs (2 of 4)

Outline Invoice.cs (3 of 4)

Outline Invoice.cs (4 of 4)

Outline Employee.cs (1 of 2)

Outline Employee.cs (2 of 2)

Outline SalariedEmployee .cs (1 of 2)

Outline SalariedEmployee .cs (2 of 2)

Software Engineering Observation 11.6 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 derived classes of a class that implements an interface also can be thought of as an object of the interface type.

Software Engineering Observation 11.7 The is-a relationship that exists between base classes and derived classes, 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 base class or interface type, the method polymorphically processes the object received as an argument.

Software Engineering Observation 11.8 All methods of class object can be called by using a reference of an interface type—the reference refers to an object, and all objects inherit the methods of class object.

Outline PayableInterfaceTe st.cs (1 of 2)

Outline PayableInterfaceTe st.cs (2 of 2)

Software Engineering Observation 11.9 Use operator overloading when it makes an application clearer than accomplishing the same operations with explicit method calls.

Outline ComplexNumber.cs (1 of 3)

Outline ComplexNumber.cs (2 of 3)

Outline ComplexNumber.cs (3 of 3)

Software Engineering Observation 11.10 Overload operators to perform the same function or similar functions on class objects as the operators perform on objects of simple types. Avoid non-intuitive use of operators.

Software Engineering Observation 11.11 At least one argument of an overloaded operator method must be a reference to an object of the class in which the operator is overloaded. This prevents programmers from changing how operators work on simple types.

Outline Operator Overloading.cs (1 of 2)

Outline Operator Overloading.cs (2 of 2)