Lecture 7
Classes and Objects
What is an Object?
An Object has two primary components:
state – properties of the object
behavior – operations the object can perform
Examples
object state behavior
dog isHungry eat, bark
grade book grades mean, median
light on/off switch
Objects in Java
A class defines a new type of Object
To create a Object type to represent a
light switch . . .
class LightSwitch {
// state and behavior here
}
Fields
An Object's state is stored in variables
called fields
Fields are declared (and optionally
initialized) inside the braces of the class
Light switch example with a field . . .
class LightSwitch {
boolean on = true;
}
Methods
An Object's behavior is defined by its
methods
Methods, like fields, are written inside
the braces of the class
Methods can access the fields (the state)
of their object and can change them
Light Switch Example
class LightSwitch {
boolean on = true; // field
boolean isOn() { // returns
return on; // the state
}
void switch() { // changes
on = !on; // the state
}
}
Constructing Objects
We use the new keyword to construct a
new instance of an Object
We can assign this instance to a variable
with the same type of the Object
LightSwitch ls = new LightSwitch();
Note: classes define new datatypes !
Using Fields and Methods
LightSwitch ls = new LightSwitch();
To access the field of an instance of an
Object use instance.field
ls.on;
To access the method of an instance use
instance.method(arguments)
ls.isOn();
ls.switch();
Example Using Light Switch
What does this main method print out?
LightSwitch ls = new LightSwitch();
System.out.println(ls.on);
ls.switch();
System.out.println(ls.isOn());
Person Example
class Person {
String name;
int age;
String getName() { return name; }
void setName(String n) { name = n; }
int getAge() { return age; }
void setAge(int a) { age = a; }
}
Constructing Person Objects
To create an instance of the Person class
with a name of "George" and an age of 22
Person obj = new Person();
obj.setName(“Abebe");
obj.setAge(22);
Can we create a Person that has the name
Abebe and the age 22 from the moment it
is created?
Constructors
Constructors are special methods used to
construct an instance of a class
They have no return type
They have the same name as the class of the
Object they are constructing
They initialize the state of the Object
Call the constructor by preceding it with the
new keyword
Person Constructor
class Person {
String name;
int age;
Person(String n, int a) {
name = n;
age = a;
}
// . . .
}
Now we can construct George as follows:
Person george = new Person("George", 22);
Default Constructor
When you do not write a constructor in a
class, it implicitly has a constructor with no
arguments and an empty body
class LightSwitch {
// Leaving out the constructor
// is the same as . . .
LightSwitch() {}
}
Result: every class has a constructor
Multiple Constructors
A class can have multiple constructors
class LightSwitch {
boolean on;
LightSwitch() {
on = true;
}
LightSwitch(boolean o) {
on = o;
}
}
This Keyword
Instance can refer to itself with the
keyword this
class LightSwitch {
boolean on;
LightSwitch() {
this.on = true; //(same as
on=true;)
}
LightSwitch(boolean on) {
this.on = on;
}
Cascading Constructors
A constructor can call another constructor with
this(arguments)
class LightSwitch {
boolean on;
LightSwitch() {
this(true);
}
LightSwitch(boolean on) {
this.on = on;
}
}
Classes Recap
Classes have fields to store the state of the objects
in the class and methods to provide the operations
the objects can perform.
We construct instances of a class with the keyword
new followed by a call a constructor method of the
class.
If you do not provide a constructor, the class will
have one with no arguments and no statements by
default.
Equality Quiz 1
Is (a == b) ?
int a = 7;
int b = 7;
Is (g == h) ?
Person g = new Person(“Abebe", 26);
Person h = new Person(“Abebe", 26);
Primitives vs Objects
Two datatypes in Java: primitives and objects
Primitives: byte, short, int, long, double, float,
boolean, char
== tests if two primitives have the same value
Objects: defined in Java classes
== tests if two objects are the same object
References
The new keyword always constructs a
new unique instance of a class
When an instance is assigned to a
variable, that variable is said to
hold a reference or point to that object
Person g = new Person(“Abebe", 26);
Person h = new Person(“Abebe", 26);
g and h hold references to two different
objects that happen to have identical state
Reference Inequality
g != h because g and h hold references
to different objects
Person g = new Person(“Abebe", 26);
Person h = new Person(“Abebe", 26);
g h
“Abebe" “Abebe"
26 26
Equality Quiz 2
true or false?
Person g = new Person(“Abebe", 26);
Person h = new Person(“Abebe", 26);
Person g1 = new Person(“Alemu", 23);
Person g2 = g1;
a) g == h
b) g.getAge() == h.getAge()
c) g1 == g2
d) g1.getAge() == g2.getAge();
Reference Equality
g1 == g2 because g1 and g2 hold
references to the same object
Person g1 = new Person(“Alemu", 23);
Person g2 = g1;
g1
“Alemu"
23
g2
Apple Example
class Apple {
String color;
double price;
Apple(String color, double price) {
this.color = color;
this.price = price;
}
Apple(double price) {
this("green", price);
}
String getColor() { return color; }
double getPrice() { return price; }
void setPrice(double p) { price = p; }
}
Apple Quiz
What will these lines print out?
Apple a = new Apple("red", 100.0);
System.out.println(a.getColor());
System.out.println(a.getPrice());
a.setPrice(50.5);
System.out.println(a.getPrice());
Apple b = new Apple(74.6);
System.out.println(b.getColor());
System.out.println(b.getPrice());
b.setPrice(a.getPrice());
System.out.println(b.getPrice());
Java API
You can get information on all in-built Java
classes/methods by browsing the Java
Application Programming Interface (API)
This documentation is essential to building
any substantial Java application