Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005.

Slides:



Advertisements
Similar presentations
Inheritance Writing and using Classes effectively.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance. Introduction In real situations either when modeling real world objects such as vehicles, animals, etc. or when modeling abstract data structures.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Writing Classes (Chapter 4)
Inheritance in C++ Content adapted from a lecture by Dr Soo Yuen Jien.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Inheritance and Access Control CS 162 (Summer 2009)
Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February Objectives of this presentation:
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Copyright 2010 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading: 9.1.
TCP1201 OOPDS 1 Inheritance. 2 Learning Objectives To understand inheritance terminology To understand multiple inheritance To understand diamond inheritance.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
CS1020 Lecture Note #7: Object Oriented Programming Inheritance Like father, like son.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming Part 3 Bank Account Embezzling
Presentation transcript:

Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February

2 Object-Oriented Programming Part 2 This presentation will cover: 1.Statement of Bank Problem 2.Proposed Solutions 3.Inheritance 4. BankAcct class 5. CheckAcct class 6. SavAcct class

3 Bank Problem Problem: Hood National Bank has hired you to develop banking software. Currently, the bank has several types of bank accounts (checking, savings, CD, money market, etc.) Write a program that will satisfy your customer’s needs. CheckingSavingsMoney Market A checking account allows you to deposit money but incurs a transaction charge of 50 cents per withdrawal transaction. This 50 cent surcharge is universal across all checking accounts. A savings account has no transaction charges and yields an interest rate, based on the lowest balance during a compounding period. The interest rate may differ across savings accounts. A money market account is essentially a savings account that requires a minimum balance of 1000 dollars at all times. Every withdrawal transaction that leaves the balance below 1000 dollars triggers a penalty of 10 dollars.

4 Proposed Solution Here’s one possible approach: Create a class for each type of bank account: class CheckAcct {.. } class SavAcct {.. } class MMAcct {.. } Disadvantage: A lot of the code is apt to be very similar. For instance, all classes contain instance fields acctName and balance, and method getAcctNo() and getBalance(). This code would have to be duplicated for each type of account! If the bank offers new types of accounts in the future, then every software application (including the one you’re currently writing) must be updated with the new type of account. Maintenance nightmare.

5 Another Proposed Solution Create a master class Acct that accommodates all account types: public class Acct { private double balance; private String acctNo; private int type; // 1=Chkng. 2=Svng. 3=MM. private double intRate; // needed for Svng, MM private double minBal; // needed for MM.. public void withdraw(double amt) { if (type == 0) {.. } else if (type == 1) }.. }

6 Another Proposed Solution (cont’d) Advantage: Some duplicated code can now be avoided. Disadvantages: Since the master class Acct must accommodate any type of account, it has an excess of instance fields. An instance field must be included even if only one type of account needs it. A maintenance problem still exists. If, in the future, the bank decides to offer new types of accounts, then the Account class must be updated with the new type of account.

Best solution The solution in Java is to use inheritance.

8 Inheritance Bank Account CheckingSavings Money Market At the top of the hierarchy, the class BankAcct serves as a parent class (or base class or super class). BankAcct has two child classes (or subclasses) SavingAcct and CheckAcct. The SavingAcct class in turn has one child: MMAcct.

9 Inheritance Roughly, the inheritance diagram can be coded in Java as follows: public abstract class BankAcct {.. } public class SavAcct extends BankAcct {.. } public class CheckAcct extends BankAcct {.. } public class MMAcct extends SavAcct {.. }

10 Inheritance (cont’d) Each generation is related to the previous one via an “is-a” relationship. –A savings account and a checking account “is-a” bank account. –A Money Market account “is-a” savings account. A child class “inherits” all of the methods and instance fields from the parent class. It is as if all of the public methods from the parent class were copied and pasted into the child class! This results in less code duplication. To indicate that one class is a subclass of another we use the keyword extends; e.g. public class MMAct extends SavAcct Bank Account CheckingSavings Money Market

11 BankAcct Class The base class BankAcct is a very simple one. constructornoneString acct double bal BankAcct() void String double Return TypeComment double amtwithdraw() double amtdeposit() nonegetAcctNo() nonegetBalance() ParametersMethod String double Data Type private acctNo balance AccessInstance Variables

12 CheckAcct Class The CheckAcct class will automatically inherit the instance fields acctNo and balance from the parent class BankAcct. The CheckAcct class will automatically inherit the methods deposit() and withdraw() from BankAcct. However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a CheckAcct object since doing so will decrease the instance field balance by an additional 50 cents. In this case, we must override the definition of withdraw().

13 CheckAcct Class (cont’d) constructornoneString acct double bal CheckAcct overridevoiddouble amtwithdraw() inheritedvoiddouble amtdeposit() inheritedStringnonegetAcctNo() inheriteddoublenonegetBalance() CommentReturnTypeParameterMethod String double Data Type inherited acctNo balance CommentInstance Variables

14 CheckAcct Class (cont’d) public class CheckAcct { // Constructor public CheckAcct(String acct, double bal) {.. } // overrides withdraw() public void withdraw(double amt) {.. } // other methods and instance fields are inherited }

15 CheckAcct Class (cont’d) First, we override the withdraw() method. Here is a first attempt: // First attempt public void withdraw(double amt) { balance = balance – amt; balance = balance – 0.5; } The above, however, won’t compile! The reason is because the instance field balance is marked private in class BankAcct. This means that balance can be accessed only from non-static methods of BankAcct. Since the withdraw() method above is a method of CheckAcct (and not BankAcct), balance is inaccessible.

16 CheckAcct Class (cont’d) Here’s a second attempt to override the withdrawal() method: // second attempt public void withdraw(double amt) { withdraw(amt); withdraw(0.50); } Exercise: Why won’t the above work? withdraw will call the CheckAcct withdraw method, create an infinite recursive loop.

17 CheckAcct Class (cont’d) Here’s our third and final (and correct) attempt: // third attempt public void withdraw(double amt) { super.withdraw(amt); super.withdraw(0.50); } The keyword super is a reference to the activating object’s superclass. Think of it as a pronoun that says, “My parent”.

18 CheckAcct Class (cont’d) Finally, we define the constructor for CheckAcct class. In the constructor, we simply want to initialize the acctNo and balance as appropriate. Our first attempt looks like this: public CheckAcct(String acct, double bal) { acctNo = acct; balance = bal; } Exercise: Unfortunately, this won’t work! Why? acctNo and balance are private variables of the super class

19 CheckAcct Class (cont’d) Here’s the correct version of the constructor: public CheckAcct(String acct, double bal) { super(acct, bal); } As before, the super keyword is a pronoun for “activating object’s parent.” In this context, the super keyword is used to invoke the activating object’s parent’s constructor, i.e. BankAcct ’s constructor.

20 CheckAcct Class (cont’d) Here’s what the CheckAcct class looks like: public class CheckAcct extends BankAcct { // constructor public CheckAcct(String acct, double bal) { super(acct, bal); } // overrides withdraw() public void withdraw(double amt) { super.withdraw(amt); super.withdraw(0.50); } // other methods and instance fields are inherited }

21 SavAcct Class Will automatically inherit the instance field acctNo and balance from the parent class BankAcct. Requires two additional instance fields: the interest rate, intRate ; and the lowest balance during the compounding period, lowBal. Will automatically inherit the method deposit() and withdraw() from BankAcct. However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a SavAcct object since it affects the lowest balance. As with the CheckAcct class, we must override the definition of withdraw(). The class SavAcc t will also require a method addInt(), which adds interest to the account.

22 SavAcct Class (cont’d) overridevoiddouble amtwithdraw() constructornoneString acct double bal double rate SavAcct new methodvoidnoneaddInt() inheritedvoiddouble amtdeposit() inheritedStringnonegetAcctNo() inheriteddoublenonegetBalance() CommentReturn TypeParameterMethod String double Data Type inherited private acctNo Balance intRate lowBal CommentInstance Variables

23 SavAcct Class (cont’d) Here’s the constructor: public SavAcct(String acct, double bal, double rate) super(acct, bal); intRate = rate; lowBal = bal; } The method call super() means “call the constructor from the super class”, in this case, BankAcct.

24 SavAcct Class (cont’d) Exercise: Write the withdraw() method. Here’s a start: public void withdraw(double amt) { }

25 SavAcct Class (cont’d) Exercise: Write the addInt() method. Here’s a start: public void addInt() { }

26 SavAcct Class (cont’d) public class SavAcct extends BankAcct { private double intRate; private double lowBal; // Constructor public SavAcct(String acct, double bal, double rate) { super(acct, bal); intRate = rate; lowBal = bal; } // overrides withdraw() public void withdraw(double amt) { super.withdraw(amt); if (getBalance() < lowBal) { lowBal = getBalance(); }

27 SavAcct Class (cont’d) // new method public void addInt() { deposit(lowBal*intRate); lowBal = getBalance(); } // other methods and instance fields are inherited }

28 MMAcct Class We leave the remaining class, MMAcct, to the hands-on exercise.

End of Lecture