Download presentation
Presentation is loading. Please wait.
1
JAVA CLASSES
2
Class Definition: accessSpecifier class ClassName fields constructors
{ fields constructors methods } Attributes of the object Initialization of the object Actions on the object
3
Choose a name for a program that simulates a bank account:
public class BankAccount { fields constructors methods } We will be able to create objects of type BankAccount
4
Instance Fields An object stores its data in instance fields
Field: a technical term for a storage location inside a block of memory Instance of a class: an object of the class The class declaration specifies the instance fields:
5
access specifier nameOfVariable
Instance Field Syntax An instance field declaration consists of the following parts: access specifier (such as private, public or protected) type of variable (such as double, int, String) name of variable (such as balance) Each object of a class has its own set of instance fields You should declare all instance fields as private access specifier nameOfVariable
6
Create a field to store a bank account holder’s balance
public class BankAccount { private double balance; constructors methods }
7
private double balance; private int accountNumber; constructors
Suppose we modify the BankAccount class so that each bank account has an account number. How does this change/affect the instance fields? public class BankAccount { private double balance; private int accountNumber; constructors methods }
8
Static Fields private double balance; private int accountNumber;
A static field belongs to the class, not to any object of the class. Also called class field. public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; } If bankName was not static, each instance of BankAccount would have its own bank name
9
Static Fields Static fields should always be declared as private
Exception: Static constants, which may be either private or public public class BankAccount { public static final double OVERDRAFT_FEE = 5; /* Refer to it as BankAccount.OVERDRAFT_FEE */ }
10
Fields for the BankAccount class
public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; public static final double OVERDRAFT_FEE = 5; constructors methods }
11
Methods Three Types: Accessor – gets information to the user Mutator-
Behaviors or actions taken on an object of a class Three Types: Accessor – gets information to the user Mutator- changes or alters one of the fields of an object Helper- aids the program in solving problems… usually done behind the scenes
12
What are the behaviors of a BankAccount object?
mutator deposit money This method will add an amount to the balance in account withdraw money This method will add an amount to the balance in account mutator Accessor get balance This method will get the balance in account and return it to the user
13
Method Syntax: a method header consists of the following parts:
access specifier (such as private, public) return type of variable (such as double, int, String, void) method name of variable (such as deposit) list of incoming variables (parameters ) including their type method body in { } access specifier return type nameOfMethod (dataType paramterName) { method body; }
14
Methods for the BankAccount class
public class BankAccount { fields constructors public void deposit (double amount){ … } public void withdraw(double amount){ … } public double getBalance() {…} }
15
Methods for the BankAccount class let’s add another Helper method to compute the overdraft fee
public class BankAccount { fields constructors public void deposit (double amount){ … } public void withdraw(double amount){ … } public double getBalance() {…} private double overdraftFee() {…} }
16
Constructors: A constructor initializes an instance field
Three types of constructors Default-sets all the instance fields to their default values Numbers = 0, Strings = null, booleans = false Overloaded - sets the instance fields to user input Copy-uses a prewritten constructor to initialize other objects Constructor name = class name Constructor body is executed when a new object is created All the constructors will have the same name-only the parameters are different (computer can tell the difference) Constructors for us are always public
17
Constructor Syntax: public NameOfClass (dataType paramterName) {
access specifier (public) name of Class (such as BankAccount) list of parameters, including their data type for overloaded constructors constructor body in { } public NameOfClass (dataType paramterName) { constructor body; }
18
Constructors for the BankAccount class
public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; public static final double OVERDRAFT_FEE = 5; public BankAccount ( ) balance = 0; accountNumber = 0; } public BankAccount (double myBalance, int myAcctNum) balance = myBalance; accountNumber = myAcctNum; methods
19
Public Interface for the BankAccount class
public class BankAccount { private double balance; private int accountNumber; private static String bankName = “McLees Money”; public static final double OVERDRAFT_FEE = 5; public BankAccount ( ) balance = 0; accountNumber = 0; } public BankAccount (double myBalance, int myAcctNum) balance = myBalance; accountNumber = myAcctNum; public void deposit (double amount){ … } public void withdraw(double amount){ … } public double getBalance() {…} private double overdraftFee() {…}
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.