Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;



Similar documents
Chapter 13 - Inheritance

D06 PROGRAMMING with JAVA

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Lecture 15: Inheritance

ICOM 4015: Advanced Programming

Java: overview by example

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

5. Advanced Object-Oriented Programming Language-Oriented Programming

Inheritance, overloading and overriding

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

61A Lecture 16. Friday, October 11

CS193j, Stanford Handout #10 OOP 3

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Abstract Class & Java Interface

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Advanced Data Structures

Using Inheritance and Polymorphism

UML Class Diagrams. Lesson Objectives

CIS 190: C/C++ Programming. Polymorphism

Java (12 Weeks) Introduction to Java Programming Language

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

CS 111 Classes I 1. Software Organization View to this point:

Java CPD (I) Frans Coenen Department of Computer Science

Java Programming Language

D06 PROGRAMMING with JAVA

Instance Creation. Chapter

CISC 181 Project 3 Designing Classes for Bank Accounts

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

AP Computer Science Java Subset

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Object-Oriented Programming: Polymorphism

Java Interview Questions and Answers

Lecture 7: Class design for security

High-Level Language. Building a Modern Computer From First Principles.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

J a v a Quiz (Unit 3, Test 0 Practice)

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Outline of this lecture G52CON: Concepts of Concurrency

Building Java Programs

Facebook Twitter YouTube Google Plus Website

El Dorado Union High School District Educational Services

Lab 2: Swat ATM (Machine (Machine))

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Abstract Factory Pattern. Human Computer Interaction Research University of Nevada, Reno

Description of Class Mutation Mutation Operators for Java

RentersPLUS Move In Special

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Python for Rookies. Example Examination Paper

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Programming to Interfaces

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Hoare-Style Monitors for Java

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Programmation 2. Introduction à la programmation Java

An Automatic Reversible Transformation from Composite to Visitor in Java

Introduction to Object-Oriented Programming

Approach of Unit testing with the help of JUnit

Curriculum Map. Discipline: Computer Science Course: C++

AP COMPUTER SCIENCE A 2007 SCORING GUIDELINES

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Glossary of Object Oriented Terms

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

MAT 2170: Laboratory 3

MCI-Java: A Modified Java Virtual Machine Approach to Multiple Code Inheritance

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

C++ Programming Language

DEVELOPING DATA PROVIDERS FOR NEEDFORTRADE STUDIO PLATFORM DATA PROVIDER TYPES

q To close an account and transfer any remaining funds, you may need: q To change an automatic payment or withdrawal*, you may need:

Java Application Developer Certificate Program Competencies

Fundamentals of Java Programming

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Follow these five steps to make switching to Peoples First Savings Bank easy and hassle free:

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Object-Oriented Programming

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

FORMS INCLUDED QUICK SWITCH CHECKLIST

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Java SE 8 Programming

Java the UML Way: Integrating Object-Oriented Design and Programming

Implementation Aspects of OO-Languages

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Programming Fundamentals I CS 110, Central Washington University. November 2015

CSE 1020 Introduction to Computer Science I A sample nal exam

Mutual Exclusion using Monitors

INHERITANCE, SUBTYPING, PROTOTYPES in Object-Oriented Languages. Orlin Grigorov McMaster University CAS 706, Fall 2006

Transcription:

Abstract Classes Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features; Name, AccountNumber, Balance. Following operations can be performed: Deposit, Withdraw, tostring However, a Bank does not offer a single type of account. We can have Savings Account, Checking Account, etc. They share several common features, and have some differences. 1

SavingsAccount: There are restrictions on Withdraw. Allows only 3 withdraws in a month. Minimum balance must be maintained. No such restrictions to withdraw from Checking Account. Savings account gives interest. Checking account does not give any interest. How do we design classes? One single class Account? Two different classes: CheckingAcc and SavingAcc? 2

One single class Account. public class Account private String Name; private String AccNum; private int balance; private String AccType; private double InterestRate; /* Constructors and methods for deposit, withdraw, tostring... // Method for Withdraw. public void withdraw(int amount) { // Check if the type is saving or Checking.. // Do appropriate operations. public void calcinterest() { //Do this only if the account is Saving. 3

Satisfactory design? What if there are actually 10 different types of accounts each with different ways of doing withdraw? huge switch statement? What if the bank decides to offer a new type of account? Go into the code and make changes to it? 4

How about creating a class for each account? SavingAcc CheckingAcc? public class SavinAcc public class CheckingAcc Design and implement each class completely separately? However, there are several common features. Code for deposit(), tostring() is the same. Morevover, there is a relation among two accounts. Each one of them is an Account. We want to maintain that relation. They are not two completely unrelated classes. 5

First design the class: CheckingAcc. SavingAcc extends CheckingAcc. Can override withdraw() method and add calculateint() method. We have accomplished code re-use. Disadvantages? 6

Now it means every SavingAcc is also a CheckingAcc!!! 7

SavingAccount and CheckingAccount have some features that are common while some features are different. Both of them are accounts. Create an interface named Account. SavingsAcc and CheckingAcc implements Account? Account has methods withdraw, deposit, tostring etc.. 8

Disadvantages? Code duplication. Method deposit is same for both classes. In future, changing implementation of deposit in SavingsAcc may also cause to change implementation of deposit in CheckingAcc. 9

A better alternative. Create a Class Named Account. Extend it with SavingsAcc and CheckingAcc. How should the class Account look like? 10

public class Account { private String Name; private String AccNum; private int Balance; /* Constructor */ Code for constructor...... /* Method deposit */ public void deposit(double amount) { balance = balance + amount; /* Method tostring */ Code for tostring...... /* Method for withdraw */ Code for withdraw.... 11

Well, what should be the code for withdraw()? We do not have sufficient information at this point. The code for this method depends on the type of the account. How do we handle this? Similar for tostring 12

How about the following? public void withdraw(int amount) { Method with empty body!!!! In classes SavingAcc and CheckingAcc override the method withdraw(). Rely on Polymorphism. This way of doing things is almost good.. 13

With this one can write a driver program that has the following statement. Account a = new Account(----, ----, ----); a.withdraw(100); This is fine. But what does this mean? We are creating an Account. However, there is no such concrete thing as Account. We have Checking Account, Savings Account. When you go to a bank. open and Account. You don t just You open either Checking or Saving Account. What is the meaning of a.withdraw()? 14

Account is an abstract concept. We know intuitively what it means. But we do not have complete information. SavingsAccount and CheckingAccount are more concrete. Now we have complete information. This leads to the notion of abstract classes. 15

Account is an abstract concept. We know intuitively what it means. But we do not have complete information. SavingsAccount and CheckingAccount are more concrete. Now we have complete information. This leads to the notion of abstract classes. 16

abstract public class Account { private String Name; private String AccNum; private int Balance; /* constructor */ /* Methods that can be defined properly */ public void deposit(int amount) {...... Write code... abstract public void withdraw(int amount); abstract public void calcinterest(); abstract public String tostring(); 17

abstract public class Account We are defining an abstract class with name Account abstract is a keyword. abstract public void withdraw(int amount); This class has an abstract method named withdraw(). Note the ; at the end. This method has no body. An abstract method should not have any body. It will be overridden in a class that extends Account. 18

Account is an abstract class. We can not create any objects of an abstract class. The following is not allowed. Account a = new Account(---, ---. ---); There is no such concrete thing as Account. So there are no objects of the class Account. An abstract generally have some abstract methods and some non-abstract (also called concrete) methods. It is not necessary that an abstract class must have an abstract method. 19

public class SavingsAcc extends Account { int NumWihthdrawls; double InterestRate; /*Override the withdraw method now */ public void withdraw(int amount) {...... Actual Code for the method public void calcinterest() {... Code for the method... public String tostring() {... Code for tostring... 20

public class CheckingAcc extends Account { /*Override the withdraw method now */ public void withdraw(int amount) {...... Actual Code for the method public void calcinterest() {... Code for the method... public String tostring() {... Code... 21

CheckingAcc and SavingAcc extend Account They are not abstract classes. So they must override the all the abstract methods in Account. And these methods must be non-abstract methods. If A is an abstract class and B is a non abstract that extends A, then B must override all abstract methods in A. These methods must be non-abstract methods. However, if B itself is an abstract class, then it need not override abstract methods of A. 22

Now we can create objects of type CheckingAcc and SavingAcc SavingsAcc s = new SavingsAcc(); CheckingAcc a = new CheckingAcc(); s.withdraw() and a.withdraw() work the way they are supposed to work. 23

Now there is a sibling relation between CheckingAcc and SavingAcc. If the bank offers a new type of Account, then we can create a class corresponding to that account. 24

Account a = new Account() is not allowed. SavingsAccount s = new SavingsAccount() is allowed. Account a = new SavingsAccount() is allowed. Type of a is Account. It is pointing to an object of type SavingsAccount. Since SavingsACcount extends Account, SavingsAccount is a subtype of Account. So a variable of type Account can point to an object of type SavingsAccount. Though there are no objects of type Account. Useful in writing programs that deal with all accounts (iirespective of different types of account). 25

Account a = new SavingsAccount() Account b = new new CheckingACcount(). a.deposit(500); b.deposit(400) method deposit is defined in class Account. Its not overridden in its subclasses. a.withdraw(500); b.withdraw(200) Which withdraw is executed? Polymorphism: Look at the object type. 26

Account a; SavingsAccount s; CheckingAccount c; a = new SavingsAccount(); s = new Account(); c = new savingsaccount(); a = s; s = a; s = c; Which of the statemenst are valid? 27

More examples: SalariedEmployee, HourlyEmployee: Abstract class would be Employee. 28