[When asked] "...why is it called a class?" ... I reply "Because that's
we teach objects how to do things".
A class is a blueprint
for a collection of objects. A class describes a
of objects that have similar attributes and behaviors.
Example: Dog is a real-world
Every class has associated data, or attributes.
Attributes for Dog
breed, color, height, weight, gender, name
Every class has associated actions.
Actions for Dog class: bark,
Instances of the Dog class, or objects in the Dog class:
my dog Otis,
dog, former president Bill Clinton's dog Buddy
If we translate the Dog class into a Java class, we get this:
// In order to be
able to create specific Dog instances,
objects of type Dog, we need
// a method (a
constructor) that creates an
theName, char theGender)
public void bark()
public void sleep()
public void eat()
} // end of Dog class
Now if I'm writing a program, I can create a Dog object:
Dog otis = new Dog("Otis", 'm');
// this object is a software
of my dog
I can call the methods that were defined in the Dog class on my Dog
otis.bark(); // output to screen:
To create an instance of any class:
Use the new operator and invoke the
new ClassName(... any needed
to assign the address of this new
object to a reference variable of type ClassName:
ClassName myRefVar = new ClassName(arguments);
The use of the new keyword causes the creation, or construction, of a
object. The object is an instance of the specified class. When new is
a constructor for the class is called.
A method definition contains:
1. An access specifier (like
-- determines which other methods can call
-- most methods are declared public, so that
other methods in your program can
2. The return type of the
method (like void - which means no value is
- or int, String, etc)
-- some methods return a value
Ex: The method Character.isDigit() returns a boolean value
-- a return type of void means that the
executes some statements without
returning a value
3. The name of the method
(such as bark)
4. A list of the parameters of
the method, enclosed in parentheses (our
constructor takes the parameters theName, theGender)
-- some methods take in other information
-- the constructor for Dog takes the Dog's
and gender, and uses those values
in the body of
5. The body of the method,
which is enclosed in curly braces - a
accessSpecifier returnType methodName (parameterType
parameterName, ... )
public class Speaker
public String sayHi()
message = "Hello Friend";
message; // this method
to the caller a reference to the String message
Note: A method's execution ends
when a return statement is executed.
return value becomes the value of the method call expression.
What can we do with our Speaker class? We can compile it. But we cannot
it, because it does not contain a main method. So we write a test class
a class with a main method that allows us to try out the Speaker class.
public class SpeakerTest
public static void
Create a new Speaker
Speaker friendly = new Speaker();
So your program consists of these 2 classes. To run your program, you:
1. Make a subfolder for your program.
2. Make two files, one per class.
3. Compile both files.
4. Run the test program.
Defining Instance Fields
Let's add some data to our Speaker class:
public class Speaker
name; // the name of the person to
String message = "Hi " + name;
An instance field contains:
1. Acess specifier (typically private)
2. Type of the variable (e.g., String, int, double)
3. Variable name
We now need to change our Speaker class so that we can construct
Speaker objects with different names:
Speaker(String personName) // a
constructor for the Speaker class
name = personName;
... // the stuff we
A constructor allows us to create instances of our class. The
has the same name as the class, and is typically public.
To create an instance of Speaker class:
Speaker mrFred = new Speaker("Mr. Fred");
mrFred.sayHi(); // output: Hi Mr. Fred
Example: Write a class that
represents a circle. Write another class to
test the Circle class.
Designing a Class
Think about the objects that will be created from the new class
type. Think about:
- what the object knows
- what the object does
For an Alarm class -
think about what an Alarm object knows and does
Example: For a
Car class -
think about what a Car object knows and does
are an object's data, i.e., the things an object knows about itself.
Things an object can do are called methods.
Note: Often our classes contain
methods that read or write the values
of the instance variables.
A Class, Start to Finish
Example: Rectangle class
Before you start coding, think about how the objects of your
should behave. What operations should a Rectangle be capable of, for
- get current length
- get current width
- set current length
- set current width
These operations that you chose should become methods in the class.
What data do you want to store for each Rectangle object?
Example: BankAccount - write a
class that represents a bank account
1. Decide what operations/methods you need
What operations do you want
carry out with a bank account?
- get balance
- deposit money
- withdraw money
If you have a BankAccount object called myChecking, you would like
be able to call methods like:
myChecking.deposit(2000); // add $2000 to my checking account
myChecking.withdraw(30); // get some cash
System.out.println(myChecking.getBalance()); // let me see
So the BankAccount class should contain the methods:
2. Figure out what information each method needs to receive (parameters), and what will be the return value, if any (what do they
In our examples, the deposit
and withdraw methods took a double value, a money amount, and did not
return a value. The getBalance method did not take any parameters, but
returned a double value, the account balance. So we have these method
public void deposit(double depositAmount)
public void withdraw(double withdrawAmount)
public double getBalance()
3. What information do we need to store about each BankAccount instance?
We need to keep track of the bank
account's balance: double balance
4. Next, let's think about the constructors - how do we want to be able
create BankAccount objects?
- The default balance is 0.
- Some nonzero initial balance.
So we could create a
BankAccount myChecking = new BankAccount(); // this should
account with a zero balance
or with this statement:
BankAccount myChecking = new BankAccount(500); // initial
- public BankAccount()
- public BankAccount(double
When we use the same method
for multiple methods that have
different parameters, we say the method name is overloaded.
The constructors and methods of a class make up the public interface of
the class. These are the operations that any code in your program can
call to create and alter BankAccount objects.
Ex: the public interface of the
BankAccount class is
public BankAccount(double initialBalance)
public void deposit (double depositAmount)
public void withdraw(double withdrawAmount)
public double getBalance()
The behavior of our BankAccount class allows you to carry out all the
common operations on bank accounts.
Example: Using some BankAccount
objects (this code would not go in the BankAccount class - it might go
in a class called BankAccountTest, for example)
Take $3000 from Mom's
savings account and move it to my checking
double amt = 3000;
We can use objects of the BankAccount class to manipulate money without
knowing how the BankAccount objects carry out these operations.
Providing the user of a class with the public interface and hiding the
implementation details is called encapsulation,
an important object-oriented concept.
Implementing the Class
Now that we have decided what methods and constructors we want our
class to have, and how we want to use them, we need to implement them
and add necessary instance fields to our class.
What data do we need to store for our bank account objects? The balance.
Example: BankAccount class
public class BankAccount
balance; // only allow access
to the balance through the getBalance() method. Only the BankAccount
// methods and constructors can
// The balance is a private implementation detail of this class - this
BankAccount() // create a bank account
with a 0 balance
balance = 0;
BankAccount(double initialBalance) //
create a bank account with the specified balance
balance = initialBalance;
deposit (double depositAmount)
balance += depositAmount;
withdraw (double withdrawAmount)
withdrawAmount) >= 0) // do not allow account to be overdrawn
when I refer to an
instance variable like balance in my code, I am referring to the
balance of the current object.
Example: BankAccountTest class
public class BankAccountTest
// This class
tests the BankAccount class
void main(String args)
= new BankAccount(); // initial zero balance
myChecking.deposit(2000); // deposit a check for $2000
balance is: " + myChecking.getBalance());
BankAccount momSavings = new
Steps in creating a class:
- Consider what you need to do
with instances of
the class - what
operations do you need for the current application? For example, if you
are writing a Vehicle class, you cannot possibly include all attributes
of Vehicles, or all possible operations of Vehicles - there are too
many. You must choose the operations and attributes you need for the
given problem you are solving - this is called abstraction. Abstaction is an
important OO concept.
- Choose method names, and
parameter types and return
type for all methods.
- Write documentation that
indicates exactly what
each method does.
- Determine what instance
variables you need.
Determine the type
and names of these instance variables.
- Determine what constructors
you need for
objects of this class.
- Implement the methods.
- Test the class.
Exercise: Write a class that
represents a coin that can be flipped.
Exercise: Write a class to test
your Coin class. This class should prompt the user to choose either
heads or tails. Then it should toss a coin and tell the user if they
won or not.