Implementing Classes
description
Transcript of Implementing Classes
![Page 1: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/1.jpg)
IMPLEMENTING CLASSESChapter 3
![Page 2: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/2.jpg)
Black Box
Something that magically does its thing! You know what it does but not how. You really don’t care how. Example – car Its interaction with the outside world is
know. You know how to interface with it.
![Page 3: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/3.jpg)
Encapsulation
The hiding of unimportant details Black box provides encapsulation Somebody has to come up with the
concept. Software uses encapsulation to take
complex routines and form “black boxes” Object-oriented programming
Black-boxes from which a program is manufactured are called objects
We learn the interfaces and what they are to do but not how they do it.
![Page 4: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/4.jpg)
Abstraction
Taking away inessential features Formal definition: the process of finding
the essential feature set for a building block of a program such as a class
What we know What it does How to interface with it
What we don’t how How it does it
![Page 5: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/5.jpg)
Classes
Designer must understand the problem Designer must understand the behavior
of the class Others can use the class
They don’t need to understand the workings Must provide a means of interfacing with
class
![Page 6: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/6.jpg)
Example
We will use a bank account We will call our class BankAccount This example will be used through out
the semester and this book
![Page 7: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/7.jpg)
What to Do
Design a BankAccount class that other programmers can use (abstraction)
Find essential operations Deposit money Withdraw money Get the current balance
Programmers who use class will view its objects as black boxes
![Page 8: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/8.jpg)
What To Do
Each operation == a method Turn the essential operations into a
method or a black box
![Page 9: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/9.jpg)
What To Do
Methods needed public void deposit(double amount) public void withdraw(double amount) public double getBalance()
Which are accessors? Which are mutators?
![Page 10: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/10.jpg)
What to Do
When we want to use one of the methods we must call it.
Example of methods calls harrysChecking.deposit(2000) harrysChecking.withdraw(500) System.out.println(harryChecking.getBalance()
)
![Page 11: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/11.jpg)
Methods
Every method contains: An access specifier (usually public) The return type
Void (no return) Type (int, double, String)
Name of the method List of the parameters () or (something)
![Page 12: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/12.jpg)
Methods
public void deposit(double amount){
method body}public void withdraw(double amount)public double getBalance( )
![Page 13: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/13.jpg)
Contructors
Contain instructions to initialize objects Resemble methods When you create an object the
constructor is called
BankAccount harrysChecking = new BankAccount ();
BankAccount harrysChecking = new BankAccount (5000);
![Page 14: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/14.jpg)
Constructor
Difference between constructor and method Name of constructor is the same as the class Have not return type not even void
![Page 15: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/15.jpg)
Creating Constructors
public class BankAccount{
// Constructorspublic BankAccount(){Fill in later}public BankAccount(double
initialBalance){Fill in later}
}
![Page 16: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/16.jpg)
Instance Field
An object stores its data in instance fields Instance fields are the variables
associated with the object Field – storage location within a block of
memory Instance – the object of the class
![Page 17: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/17.jpg)
Instance Field
Instance field declaration consists of: An access specifier (usually private) The type of the instance field Name of the instance field
![Page 18: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/18.jpg)
Instance Field Declaration
public class BankAccount{
…..private double balance;…..
}
Every object of BankAccount will have a field named balance
![Page 19: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/19.jpg)
What Does Private Mean
Instance fields are hidden from the programmer who uses the class
They are only of concern to the programmer who implement the class
All access must occur through public methods not instance fields
The process of hiding data and providing methods for data access is called encapsulation
![Page 20: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/20.jpg)
Instance Fields
![Page 21: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/21.jpg)
Access to Instance Fields
Separate method Example: getBalance
Returns the balance Can grant the user access to only get the
balance the not change the balance You control who has access to what
![Page 22: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/22.jpg)
Implementing Constructors
Constructors contain instructions to initialize the instance fields of an objectpublic BankAccount(){ balance = 0;}public BankAccount(double initialBalance){ balance = initialBalance;}
![Page 23: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/23.jpg)
Constructor Call Example
Create a new object of type BankAccount Call the 2nd constructor (since the construction
parameter supplied matches the type of 2nd ) Set the parameter variable initialBalance to
1000 In the constructor the balance instance field of
the newly created object is set to initialBalance
BankAccount harrysChecking = new BankAccount(1000.0);
![Page 24: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/24.jpg)
Constructor Call Example
Return an object reference, that is, the memory location of the object, as the value of the new expression
Store that object reference in the harrysChecking variable
BankAccount harrysChecking = new BankAccount(1000);
![Page 25: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/25.jpg)
Instance Fields
1000.00
![Page 26: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/26.jpg)
File BankAccount.java01: /**02: A bank account has a balance that can be changed by 03: deposits and withdrawals.04: */05: public class BankAccount06: { 07: /**08: Constructs a bank account with a zero balance.09: */10: public BankAccount()11: { 12: balance = 0;13: }14: 15: /**16: Constructs a bank account with a given balance.17: @param initialBalance the initial balance 18: */ Continued…
![Page 27: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/27.jpg)
File BankAccount.java19: public BankAccount(double initialBalance)20: { 21: balance = initialBalance;22: }23: 24: /**25: Deposits money into the bank account.26: @param amount the amount to deposit27: */28: public void deposit(double amount)29: { 30: double newBalance = balance + amount;31: balance = newBalance;32: }33: 34: /**35: Withdraws money from the bank account.36: @param amount the amount to withdraw Continued…
![Page 28: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/28.jpg)
File BankAccount.java37: */38: public void withdraw(double amount)39: { 40: double newBalance = balance - amount;41: balance = newBalance;42: }43: 44: /**45: Gets the current balance of the bank account.46: @return the current balance47: */48: public double getBalance()49: { 50: return balance;51: }52: 53: private double balance;54: }
![Page 29: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/29.jpg)
Testing a Class
We need to be sure our Class (in this case BankAccount) works correctly
Write a test case or tester class Construct one or more objects of the class that
is being tested Invoke one or more methods Print out one or more results Print the expected results
![Page 30: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/30.jpg)
BankAccountTester.java01: /**02: A class to test the BankAccount class.03: */04: public class BankAccountTester05: {06: /**07: Tests the methods of the BankAccount class.08: @param args not used09: */10: public static void main(String[] args)11: {12: BankAccount harrysChecking = new BankAccount();13: harrysChecking.deposit(2000);14: harrysChecking.withdraw(500);
System.out.println(“Expected result: 1500”);15: System.out.println(harrysChecking.getBalance());16: }17: }
![Page 31: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/31.jpg)
Categories of Variables
Categories of variables Instance fields (balance in BankAccount) Local variables (newBalance in deposit
method) Parameter variables (amount in deposit
method) An instance field belongs to an object
The fields stay alive until no method uses the object any longer
![Page 32: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/32.jpg)
Categories of Variables
Local & parameter variable Local variables must be initialized Parameter variables are initialized in the
method call Instance fields that are numbers are
initialized to zero by default Object references are set to “null” by
default
![Page 33: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/33.jpg)
Categories of Variables
In Java, the garbage collector periodically reclaims objects when they are no longer used
Local and parameter variables belong to a method
![Page 34: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/34.jpg)
Lifetime of Variables
harrysChecking.deposit(500);double newBalance = balance + amount;balance = newBalance;
Continued…
![Page 35: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/35.jpg)
Lifetime of Variables
Figure 7: Lifetime of Variables Continued…
![Page 36: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/36.jpg)
Lifetime of Variables
Figure 7: Lifetime of Variables
![Page 37: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/37.jpg)
Implicit and Explicit Method Parameters
The implicit parameter of a method is the object on which the method is invoked
momsSavings.withdraw(500);
Sometime you will see the word this used as a reference and denotes the implicit parameter
Implicit parameter
![Page 38: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/38.jpg)
Implicit and Explicit Method Parameters
Use of an instance field name in a method denotes the instance field of the implicit parameter
public void withdraw(double amount){ double newBalance = balance - amount; balance = newBalance;}
![Page 39: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/39.jpg)
Implicit and Explicit Method Parameters
balance is the balance of the object to the left of the dot:
means
double newBalance = momsSavings.balance - amount;momsSavings.balance = newBalance;
momsSavings.withdraw(500)
![Page 40: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/40.jpg)
Implicit Parameters and this
Every method has one implicit parameter The implicit parameter is always called this
Exception: Static methods do not have an implicit parameter (more later) – remember main (no object)
double newBalance = balance + amount;// actually meansdouble newBalance = this.balance + amount;
![Page 41: Implementing Classes](https://reader035.fdocuments.us/reader035/viewer/2022062811/568161cf550346895dd1c230/html5/thumbnails/41.jpg)
Implicit Parameters and this
Figure 8:The Implicit Parameter of a Method Call