Classes
Classes
Java Classes
GEEN163 Introduction to Computer Programming
Object-oriented programming uses classes, objects, and methods as basic programming components. These components help to
organize a large program into small modules design and think about an intricate program find and remove errors (bugs)
Nomenclature
Class defines a kind of object Object an instance of a class. Instantiate creating an object of a class Instance An object is an instance of a class Method coordinated sequence of instructions or function that an object can perform.
Objects
An object is an instance of a class. Class: String Objects: String firstname, lastname, abc, xyz;
GEEN163
Class Definitions
Declaring an Object
Imagine we have a class Widget We can declare an object of type Widget just like we declare a variable to be an int or a double. Widget Widget thing; dodad, whatchamacallit;
GEEN163
Class Definitions
Reference Variables
When you declare a primitive data item, such as a double or int, Java reserves some memory to store the data. When you declare an object, Java does not reserve space for the object until it is created. You can instantiate a new object with the keyword new.
Instantiating Objects
Widget something = new Widget(); Widget zebra; zebra = new Widget(); After the word new is a call to a constructor method that helps create the object. The constructor method may or may not have parameters.
Field Declaration
Field variables are declared within the class definition, but outside of any method. public class Student { int identifier; public double grade; public String name; }
GEEN163
Class Definitions
GEEN163
Class Definitions
Data Abstraction
Classes are described by their interface, that is, what they can do and how they are used. The internal data and operation are hidden. In this way if the internal operation is changed, programs using the class will not be impacted as long as the interface remains consistent.
Java Methods
Class methods can use the class variables and any variables defined in the method. Class methods provide the actions of the object. Methods appear inside the class definition.
Method Purpose
Constructors
Initialize an object
Constructors
A constructor method is automatically called when an object is created. The name of the constructor method is always the same as the class name. Constructors can be used to initialize the values of an objects variables. Constructors may or may not have parameters.
Modifiers
Change the values of an object
Accessors
Return the value of a object without changing anything
Function
Compute some value or perform some action
GEEN163
Class Definitions
Accessor Methods
Accessor methods return some value from the object. Accessor methods do not change anything in the object. Examples of accessor methods include:
String length method.
Modifier Methods
Modifier methods change the state of an object. A modifier method may just set the value of a single variable or may perform a lengthy sequence of actions. Examples of modifier methods include:
setGrade from the earlier example
Naming Convention
Class names start with an uppercase letter Objects are written in lowercase Method names are in lowercase Constructors have the same name as the class Accessor methods have names that can be used as nouns Modifier methods have names that can be used as verbs
Calling Methods
In Java you can use a method of an object by writing the objects name, a period, the name of the method. Widget thing = new Widget(); thing.whatever( 5 ); This calls the whatever method of Widget object thing passing it an integer 5 as a parameter
GEEN163
Class Definitions
BankAccount example
The BankAccount class represents a simple account in a bank. There are two data items
The current balance The name of the account owner
BankAccount Methods
Constructor method
BankAccount(double initial, string name)
Accessor methods
double balance() // returns the current balance string who() // returns the owner name
Although there is only one BankAccount class, there can be many BankAccount objects.
Modifier methods
void deposit(double cash) void withdraw(double cash) // add to balance // reduce balance
/* Example of bank account class */ public class BankAccount { double money; String owner; // constructor public BankAccount( double initial, String name) { money = initial; owner = name; } /* method to return the balance of the account */ public double balance() { return money; }
/* method to return the name of the owner */ public String who(){ return owner; } /* method to add money to the balance */ public void deposit(double cash) { money = money + cash; } /* method to remove money from the balance */ public void withdraw(double cash) { money = money - cash; } };
// end of BankAccount class
GEEN163
Class Definitions
public static void main (String[] args) { BankAccount savings = new BankAccount (100.00, "Ken"); savings.deposit(50.00); System.out.println("Money in account is " + savings.balance() ); }
GEEN163