 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.

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.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
 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.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
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.
Slide 1 Polymorphism: Part I. Slide 2 Summary: Derived Class * ‘is-a’ relationship n Different from ‘has-a’ or ‘uses-a’, or … by class-in-class * Public.
Chapter 8 – Sections covered
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.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Cpt S 122 – Data Structures Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
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.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
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ü.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
CMSC 202 Inheritance I Class Reuse with Inheritance.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using 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.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
 2009 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
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.
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading
Polymorphism, Interfaces & Operator Overloading
Classes and Inheritance
Polymorphism, Interfaces & Operator Overloading
abstract classes and casting objects
Object-Oriented Programming: Polymorphism
Abstract Classes.
Chapter 9 Inheritance and Polymorphism
“Java - How to Program” (6th) by Deitel & Deitel
Object-Oriented Programming: Polymorphism
Inheritance I Class Reuse with Inheritance
Abstract Classes AKEEL AHMED.
Advanced Java Topics Chapter 9
CMSC 202 Inheritance.
Class Reuse with Inheritance
METHOD OVERRIDING in JAVA
Chapter 7 Inheritance.
Presentation transcript:

 Simple payroll application that polymorphically 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).

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

Base class Derived class

Same

 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%.

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

 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 { override public void G() { Console.WriteLine("C.G"); }

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

implements