Chapter 4 Classes & Objects
Chapter 4 Classes & Objects
Examples
class LightSwitch {
boolean on = true;
true
false
AUHHC oop lecture slides 9
Person Example
class Person {
String name = “Jamal”;
int age = 26;
String getName() {
return name;
}
void setName(String n) {
name = n;
}
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
boolean smellsBad(){
return true;
}
}
AUHHC oop lecture slides 10
Constructing Person Objects
To create an instance of the Person class with a
name of "George" and an age of 22
Person george = new Person();
george.setName("George");
george.setAge(22);
Person(String n, int a) {
name = n;
age = a;
}
// . . .
}
Now we can construct George as follows:
Person george = new Person("George", 22);
AUHHC oop lecture slides 13
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 {
boolean on;
LightSwitch() {
on = true;
}
LightSwitch(boolean o) {
on = o;
}
} AUHHC oop lecture slides 15
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;
}
}
AUHHC oop lecture slides 16
Cascading Constructors
A constructor
can call another constructor with
this(arguments)
class LightSwitch {
boolean on;
LightSwitch() {
this(true);
}
LightSwitch(boolean on) {
this.on = on;
}
AUHHC oop lecture slides 17
}
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.
We can assign an instance to a variable with a
datatype named the same as the class.
If you do not provide a constructor, the class will
have one with no arguments and no statements by
default. AUHHC oop lecture slides 18
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;
}
}
AUHHC oop lecture slides 19
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()); red
a.setPrice(50.5); 100.0
System.out.println(a.getPrice());
Apple b = new Apple(74.6); 50.5
System.out.println(b.getColor());
System.out.println(b.getPrice());
b.setPrice(a.getPrice()); green
System.out.println(b.getPrice()); 74.6
50.5
AUHHC oop lecture slides 20
Recall the LightSwitch Class
class LightSwitch {
boolean on = true;
boolean isOn() {
return on;
}
void switch() {
on = !on;
}
} AUHHC oop lecture slides 21
A Different LightSwitch Class
class LightSwitch {
int on = 1;
boolean isOn() {
return on == 1;
}
void switch() {
on = 1 - on;
}
} AUHHC oop lecture slides 22
Abstraction
Both LightSwitch classes behave the same.
We treat LightSwitch as an abstraction: we do
not care about the internal code of LightSwitch,
only the external behavior
Internalcode = implementation
External behavior = interface
// . . .
}
Now AbstractionBreaker's attempt to access
the on field would not have compiled to begin
with.
Is (g == h) ?
Person g = new Person("Jamal", 26);
Person h = new Person("Jamal", 26);
Answer: No
AUHHC oop lecture slides 29
Primitives vs Objects
Two datatypes in Java: primitives and objects
g h
"Jamal" "Jamal"
26 26
greg1
"Greg"
23
greg2
a) g == h false
b) g.getAge() == h.getAge() true
c) greg1 == greg2 true
d) greg1.getAge() == greg2.getAge(); true
AUHHC oop lecture slides 34
Java API
You can get information on all in-built Java
classes/methods by browsing the Java
Application Programming Interface (API)
Methods
When we use square root, we don’t care about these steps. All
we need is to get the correct output.
Example:
Multiple
method arguments are separated
by commas:
double pow(double x, double y)