CS1083 Week 3: Polymorphism

Similar documents
CHAPTER 10 INHERITANCE

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Inheritance (P1 2006/2007)

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Intro to Computer Science 2. Inheritance

3/7/2012. Chapter Ten: Inheritance. Chapter Goals

COSC This week. Will Learn

CSC Inheritance. Fall 2009

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Handout 9 OO Inheritance.

Introduction to Inheritance

Principles of Software Construction: Objects, Design and Concurrency. Packages and Polymorphism. toad Fall 2012

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

CSC 222: Object-Oriented Programming. Fall Inheritance & polymorphism

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

Appendix E: Using UML in Class Design

C# Programming for Developers Course Labs Contents

1. BlueJ bank example with subclasses of BankAccount 2. Transparency of UML diagram for BankAccount class hierarchy

BloomBank Financial Software Design

Abstract classes const member functions const arguments to a function Function overloading and overriding

Principles of Software Construction: Objects, Design, and Concurrency

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Function Overloading and Overriding this keyword static members inline functions Passing arguments by values and reference

Java Puzzle Ball Nick Ristuccia

Introduction to Object-Oriented Programming

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Programming a Bank Database. We ll store the information in two tables: INTEGER DECIMAL(10, 2)

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

STUDENT LESSON A5 Designing and Using Classes

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

Class-level reuse with inheritance Behavioral subtyping

Dr. Manal Helal CC316:Object Oriented Programming, Fall 2015 AASTMT College of Engineering & Technology

Ramana Isukapalli W3101: Programming Languages C++

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Object-Oriented Programming

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Inheritance and delega9on

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Inheritance: Definition

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Location: Planet Laser Interview Skills Workshop

CS5233 Components Models and Engineering

Introduction to Classes and Objects

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

ICOM 4015: Advanced Programming

W3110: Programming Languages C++ Ramana Isukapalli

Implementing Classes (P1 2006/2007)

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

Java Puzzle Ball MOOC Lab 4: Lambda Expressions

Inheritance and Subclasses

PIC 20A Number, Autoboxing, and Unboxing

Encapsulation. Mason Vail Boise State University Computer Science

Principles of Software Construction: Objects, Design, and Concurrency (Part 1: Designing Classes) Design for Reuse (class level)

The Scanner class reads data entered by the user. Methods: COSC Dr. Ramon Lawrence. Page 3. COSC Dr. Ramon Lawrence A) A = 6, B = 3

Class 09 Slides: Polymorphism Preconditions. Table of Contents. Postconditions

Implementing Classes

CS 251 Intermediate Programming Inheritance

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

Aquarium: AOP for Ruby

ENCAPSULATION AND POLYMORPHISM

Software Design and Analysis for Engineers

Eduardo M. Breijo Baullosa May 2, Assignment 17. Report

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CPSC 457: Principles of Operating Systems Assignment 2 due June 9, 2015

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Lecture Notes. Polymorphism. Polymorphism. If Polymorphism is Not Available. To be placed at:

Check out Polymorphism from SVN. Object & Polymorphism

SOFTWARE DEVELOPMENT 1. Objects III 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Creating and Using Objects

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

Inheritance. Inheritance

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CS 215 Software Design Sample Midterm Questions

G Programming Languages - Fall 2012

FDSc in ICT. Building a Program in C#

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Lecture 2: Writing Your Own Class Definition

Abstract classes and Interfaces

Polymorphism Part 1 1

CN208 Introduction to Computer Programming

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS 617 Object Oriented Systems Lecture 5 Classes, Classless World:Prototypes, Instance Variables, Class Variables, This/Self 3:30-5:00 pm Thu, Jan 17

index.pdf January 21,

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Designing Classes. Design for Reuse School of Computer Science

Today. Homework. Lecture Notes CPSC 224 (Spring 2012) Quiz 5. interfaces. exceptions. best practices. start on swing (if time) hw3 due

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Monitors CSCI 201 Principles of Software Development

The static Keyword. Lecture 2 Java Intermediate. The static Keyword. The Plan. Class Attributes The static Keyword. Class Methods The static Keyword

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

ITI Introduction to Computing II

ITI Introduction to Computing II

6.096 Introduction to C++

Transcription:

CS1083 Week 3: Polymorphism David Bremner 2018-01-18

Polymorphic Methods Late Binding Container Polymorphism

More kinds of accounts DecimalAccount BigDecimal -balance: BigDecimal +DecimalAccount() +DecimalAccount(initialDollars : long, initcents : int) +deposit(amount: BigDecimal) : void +getbalance() : BigDecimal -interestrate : BigDecimal SavingsAccount +SavingsAccount(permille: int, initialdollars: long, initialcents: int) +addinterest() : void CheckingAccount Checking fees for transactions TimeDeposit penalty for early withdrawal. TimeDepositAccount

Polymorphic method parameters p u b l i c s t a t i c v o i d main ( String [ ] args ) { AccountTest SavingsAccount momssavings = new SavingsAccount ( 5, 0, 0 ) ; TimeDepositAccount collegefund = new TimeDepositAccount ( 1, 3 ) ; CheckingAccount harryschecking=new CheckingAccount ( 0, 0 ) ; printbalance ( mom s s a v i n g s, momssavings ) ; printbalance ( t h e c o l l e g e fund, collegefund ) ; printbalance ( Harry s c h e c k i n g, harryschecking ) ; p u b l i c s t a t i c v o i d printbalance ( String name, DecimalAccount account ) { System. out. println ( The b a l a n c e o f + name + a c c o u n t i s $ +account. getbalance ( ) ) ;

CheckingAccount BigDecimal -balance: BigDecimal DecimalAccount +DecimalAccount() +DecimalAccount(initialDollars : long, initcents : int) +deposit(amount: BigDecimal) : void +getbalance() : BigDecimal -transactioncount: int CheckingAccount +deposit(bigdecimal amount) : void «override» +withdraw(bigdecimal amount) : void «override» +deductfees() : void

Combining with Overloading p u b l i c s t a t i c v o i d main ( String [ ] args ) { AccountTest. endofmonth ( momssavings ) ; // 1 endofmonth ( collegefund ) ; // 1 endofmonth ( harryschecking ) ; // 2 // 1 p u b l i c s t a t i c v o i d endofmonth ( SavingsAccount savings ) { savings. addinterest ( ) ; // 2 p u b l i c s t a t i c v o i d endofmonth ( CheckingAccount checking ) { checking. deductfees ( ) ;

Polymorphic Methods Late Binding Container Polymorphism

Late Binding Example 1 What withdraw methods are called? CheckingAccount account =new CheckingAccount ( 1 0 0 0, 0 ) ; account. withdraw ( 5 0 0 ) ; -balance: BigDecimal DecimalAccount +DecimalAccount() +DecimalAccount(initialDollars : long, initcents : int) +deposit(amount: BigDecimal) : void +deposit(amount: long): void +withdraw(amount: BigDecimal) : void +withdraw(amount: long) : void +transfer(other: DecimalAccount, amount: BigDecimal) +transfer(other: DecimalAccount, amount: long) +getbalance() : BigDecimal -transactioncount: int CheckingAccount +deposit(bigdecimal amount) : void «override» +withdraw(bigdecimal amount) : void «override» +deductfees() : void

Late Binding Example 1 DecimalAccount p u b l i c v o i d withdraw ( l o n g amount ) { t h i s. withdraw ( new BigDecimal ( amount ) ) ; CheckingAccount f i n a l p u b l i c v o i d withdraw ( BigDecimal amount ) { transactioncount++; s u p e r. withdraw ( amount ) ; note use of final

Late Binding Example 2 DecimalAccount -balance: BigDecimal +DecimalAccount() +DecimalAccount(initialDollars : long, initcents : int) +deposit(amount: BigDecimal) : void +deposit(amount: long): void +withdraw(amount: BigDecimal) : void +withdraw(amount: long) : void +transfer(other: DecimalAccount, amount: BigDecimal) +transfer(other: DecimalAccount, amount: long) +getbalance() : BigDecimal -interestrate : BigDecimal SavingsAccount +SavingsAccount(permille: int, initialdollars: long, initialcents: int) +addinterest() : void -transactioncount: int CheckingAccount +deposit(bigdecimal amount) : void «override» +withdraw(bigdecimal amount) : void «override» +deductfees() : void -periodstomaturity: int TimeDepositAccount +addinterest(): void +withdraw(bigdecimal amount): void «override» TimeDepositAccount td = CheckingAccount check = td. transfer ( check, 2 0 0 ) ;

Late Binding Example 2 DecimalAccount p u b l i c v o i d transfer ( DecimalAccount other, l o n g amount ) { withdraw ( amount ) ; other. deposit ( amount ) ; TimeDepositAccount p u b l i c f i n a l v o i d withdraw ( BigDecimal amount ) { i f ( periodstomaturity > 0) s u p e r. withdraw ( EARLY_WITHDRAWAL_PENALTY ) ; s u p e r. withdraw ( amount ) ;

Polymorphic Methods Late Binding Container Polymorphism

Polymorphic Arrays Why doesn t this work? What should we do instead? DecimalAccount [ ] accounts = new DecimalAccount [ 3 ] ; accounts [0]= momssavings ; accounts [1]= collegefund ; accounts [ 2] = harryschecking ; f o r ( DecimalAccount account : accounts ) { endofmonth ( account ) ;