Banking Service class BankingService { LinkedList accounts; LinkedList customers; double getBalance(int forAcctNum) { for (Account acct:accounts) { if.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Classes, Encapsulation, Methods and Constructors
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Writing Classes (Chapter 4)
Intro to OOP with Java, C. Thomas Wu
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Programming in Java CSCI-2220 Object Oriented Programming.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 1: Object Oriented Paradigm. 1.1 Data Abstraction and Encapsulation OOP allows programmer to – separate the details that are important to the.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
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:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Topics Instance variables, set and get methods Encapsulation
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
OOP: Encapsulation &Abstraction
Lecture 3 John Woodward.
Introduction to Classes and Objects
Exceptions, Interfaces & Generics
Computing with C# and the .NET Framework
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Encapsulation and Constructors
Anatomy of a Method.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
By Rajanikanth B OOP Concepts By Rajanikanth B
Visibilities and Static-ness
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Banking Service class BankingService { LinkedList accounts; LinkedList customers; double getBalance(int forAcctNum) { for (Account acct:accounts) { if (acct.number == forAcctNum) return acct.balance; } return 0; } double withdraw(int forAcctNum, double amt) { for (Account acct:accounts) { if (acct.number == forAcctNum) { acct.balance = acct.balance - amt; return amt; } return 0; } String login(String custname, int withPwd) { for (Customer cust:customers) { if (cust.name.equals(custname)) { if (cust.password == withPwd) return "Welcome"; else return "Try Again"; }} return "Oops -- don't know this customer"; } class Customer { String name; int password; LinkedList accounts; } class Account { int number; Customer owner; double balance; }

Problems With Code “if (cust.password == withPwd)” Should not be allowed BankingService shouldn’t be doing math regarding withdrawals, put that in Account class Relies on a linkedlist of customers and linkedlist of accounts, which spills into getBalance, withdraw, and login methods “return 0;” Is this an error or the actual balance? It could be either. These should be separated.

Problems With Code: Encapsulation “if (cust.password == withPwd)” Should not be allowed BankingService shouldn’t be doing math regarding withdrawals, put that in Account class Relies on a linkedlist of customers and linkedlist of accounts, which spills into getBalance, withdraw, and login methods “return 0;” Is this an error or the actual balance? It could be either. These should be separated.

Problems With Code: Exceptions “return 0;” Is this an error or the actual balance? It could be either. These should be separated.

Getters.getBalance() returns balance field Not directly accessing the field in another class

Access Modifiers Private: item is only accessible by name inside the class. To access field outside classes, need getter Public: every other class or object can access item Protected: objects in current class and all of its subclasses (and their subclasses) can access this item “Generally we want to make all of the fields in all of the classes private. This is a good general rule of thumb, unless you have a good reason to do otherwise.”

More Good Programming Practices Put access modifiers on every field and method (including constructors) in a class. Any method that is visible through an interface must be public. Any method that another class must use should be public. Make constructors in abstract classes protected, so subclasses can invoke them. Make constructors that can’t be used by other classes private.