Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading

Slides:



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

ITF11006.NET Inheritance. Classes and Inheritance Constructors and Inheritance Modifiers Interfaces Operators.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
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.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
 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
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CMSC 202 Inheritance I Class Reuse with Inheritance.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Inheritance ndex.html ndex.htmland “Java.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
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.
Modern Programming Tools And Techniques-I
Object-Oriented Programming: Polymorphism
Polymorphism, Interfaces & Operator Overloading
Polymorphism 2nd Lecture
Classes and Inheritance
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Polymorphism 2nd Lecture
Polymorphism, Interfaces & Operator Overloading
abstract classes and casting objects
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
Object Oriented Programming
OOP’S Concepts in C#.Net
Abstract Classes.
Chapter 9 Inheritance and Polymorphism
“Java - How to Program” (6th) by Deitel & Deitel
Polymorphism 2nd Lecture
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Object-Oriented Programming: Polymorphism
Inheritance I Class Reuse with Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Abstract Classes AKEEL AHMED.
Based on slides from Deitel & Associates, Inc.
Advanced Java Topics Chapter 9
CMSC 202 Inheritance.
Class Reuse with Inheritance
Object-Oriented Programming: Polymorphism
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Object-Oriented Programming: Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Chapter 8 Abstract Classes.
Chapter 7 Inheritance.
Computer Science II for Majors
Presentation transcript:

Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading

12.2 Polymorphism Examples

By example: Polymorphic Employee Inheritance Hierarchy Simple payroll application that polymorphically and dynamically calculates the weekly pay of several different types of employees using each employee’s Earnings method. Though the earnings of each type of employee are calculated in a specific way, polymorphism allows us to process the employees “in the general.” Two new classes — SalariedEmployee (for people paid a fixed weekly salary) and HourlyEmployee (for people paid an hourly salary and “time-and-a-half” for overtime).

By example: Polymorphic Employee Inheritance Hierarchy cont common set of functionality for all the classes in the updated hierarchy in an “abstract” class, Employee, from which classes SalariedEmployee, HourlyEmployee and CommissionEmployee inherit directly and class BasePlusCommissionEmployee inherits indirectly. => invoke each employee’s Earnings method off a base class Employee reference, the correct earnings calculation is performed due to C#’s polymorphic capabilities.

12.3 Demonstrating Polymorphic Behavior

Base class Derived class

Same

12.4 Abstract Classes and Methods

Determining the Type of an Object at Execution Time Occasionally, when performing polymorphic processing, we need to program “in the specific.” Employee case study demonstrates that an application can determine the type of an object at execution time and act on that object accordingly. In the case study, we use these capabilities to determine whether a particular employee object is a BasePlusCommissionEmployee. As the result employee’s base salary is increased by 10%.

12.5 Case Study: Payroll System Using Polymorphism

override/virtual method1_derived method2_derived method1_base public class Base {public class Base { public string method1() { return "method1_base"; } public virtual string method2() { return "method2_base"; } } public class Derived : Base public static void Main(string[] args) method1_derived Derived derived = new Derived(); Console.WriteLine(derived.method1()); Console.WriteLine(derived.method2()); Console.WriteLine(derived.method3()); Console.WriteLine(derived.method4()); public string method1() { return "method1_derived"; } //hiding public override string method2() { return "method2_derived"; } public string method3() { return base.method1(); } public string method4() { return base.method2(); } method1_derived method2_derived method1_base method2_base

sealed Methods When an instance method declaration includes a sealed modifier, that method is said to be a sealed method. If an instance method declaration includes the sealed modifier, it must also include the override modifier. Use of the sealed modifier prevents a derived class from further overriding the method. using System; class A { public virtual void F() { Console.WriteLine("A.F"); } public virtual void G() { Console.WriteLine("A.G"); class B: A sealed override public void F() { Console.WriteLine("B.F"); override public void G() { Console.WriteLine("B.G"); class C: B Console.WriteLine("C.G");

12.6 sealed Methods and Classes Sealed method in a base class cannot be overridden in a derived class. private methods implicitly sealed static methods implicitly sealed Class sealed cannot be a base class

12.7 Case Study: Creating and Using Interfaces

implements

12.8 Operator Overloading