Chapter 9: Classes with Instance Variables
Chapter 9: Classes with Instance Variables
or Classes=Methods+Variables
Asserting Java
? Rick Mercer
One class constructing three different objects, each with its own set of
values (state)
Classes
Classes are
Computing Fundamentals, Rick Mercer Franklin, Beedle and Associates, 2003
a collection of methods and data
a blueprint used to construct many objects
a great way to partition a software system
A way to implement any type
A type defines a set of values, and the allowable operations on those values
2
Putting it All Together in a Class
Even though quite different in specific methods and state, virtually all classes have these things in common:
- variables store the state of the objects - constructors initialize the state of an object - some messages modify the state of objects - some messages provide access to the state of
objects
3
General form of a Java class
public class class-name { //--instance variables private class-name identifier ; private primitive-type identifier ;
// Constructor public class-name ( parameters ) { }
//--Methods public return-type method-name-1 ( parameters ) { }
public return- type method-name-N ( parameters ) { } }
4
An Example class BankAccount
public class BankAccount { //--instance variables private String ID; private double balance;
// Constructor to initialize the state public BankAccount(String initID, double initBalance) {
ID = initID; balance = initBalance; }
// Credit this account by depositAmount public void deposit(double depositAmount) {
balance = balance + depositAmount; }
// Debit this account by withdrawalAmount public void withdraw(double withdrawalAmount) {
balance = balance - withdrawalAmount; }
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- a rigorous model of object reference identity and existence
- object reference not set to an instance of an object meaning
- the z garbage collector the openjdk developers guide
- visual basic objects and collections
- java arrays objects methods
- complete guide for events in workflows in sap ecc 6
- object dictionary pager
- chapter 9 classes with instance variables
- contextual object detection using set based classi cation
- java reference types object oriented programming
Related searches
- developmental psych chapter 9 quizlet
- chapter 9 cellular respiration test
- chapter 9 psychology test answers
- chapter 9 tom sawyer summary
- chapter 9 1 cellular respiration
- chapter 9 cellular respiration key
- chapter 9 cellular respiration answers
- chapter 9 cellular respiration answer key
- chapter 9 lifespan development quiz
- chapter 9 cellular respiration ppt
- mark chapter 9 verse 23
- mark chapter 9 niv