Objects and Classes
Object-Oriented Programming
Outline
Classes vs. objects
Designing a class
Methods and instance variables
Encapsulation & information hiding
Readings:
HFJ: Ch. 2, 3, 4.
i hc Cng ngh - HQG HN
Objects and Classes
Java program
public class Greeting {
public void greet() {
System.out.print("Hi there!");
}
}
public class TestGreeting {
public static void main(String[] args) {
Greeting gr = new Greeting();
gr.greet();
}
}
A Java program, at run-time, is a collection of objects. They
do things (their methods) and ask other objects to do things
(calling methods of others).
A Java program, when we write it, is a collection of classes
A Java library contains predefined classes that we can use in
our programs
i hc Cng ngh - HQG HN
Objects and Classes
33
Classes vs. objects
A class is a blueprint/template that is used to
construct objects.
Each object is instantiated from a class. That object
is called an instance of the class.
i hc Cng ngh - HQG HN
Objects and Classes
Designing a class
When you design a class, think about the objects
that will be created from that class
things the object knows about itself
things the object does
i hc Cng ngh - HQG HN
Objects and Classes
Designing a class
things the object knows about itself
instance variables
the object's instance variables represent its state
things the object can do
methods
the object's methods represent its behavior
i hc Cng ngh - HQG HN
Objects and Classes
Writing a class
1. Write the class
class Dog {
instance
variables
DOG
int size;
String breed;
String name;
a method
void bark() {
System.out.println("Ruff! Ruff!");
}
size
breed
name
bark()
i hc Cng ngh - HQG HN
Objects and Classes
Writing a class
2. Write a tester (TestDrive) class
with code to test the Dog class
dot notation (.)
gives access to
an object's
instance
variables and
methods
public class DogTestDrive {
public static void main(String [] args) {
Dog d = new Dog();
make a Dog object
d.name = "Bruno";
d.bark();
set the name of the Dog
}
}
call its bark() method
Information hiding is not here yet.
i hc Cng ngh - HQG HN
Objects and Classes
Writing a class
Instance variables/methods belong to an object.
Thus, when accessing them, you MUST specify
which object they belong to.
dot notation (.)
and
the object
reference
public class DogTestDrive {
public static void main(String [] args) {
Dog d = new Dog();
d.name = "Bruno";
d.bark();
access 'name' of the Dog
}
}
call its bark() method
i hc Cng ngh - HQG HN
Objects and Classes
Object references
3 steps of object declaration, creation and assignment:
1. Declare a reference variable
Dog myDog = new Dog();
2. Create an object
Dog myDog = new Dog();
3. Link the object and the reference
Dog myDog
i hc Cng ngh - HQG HN
new Dog();
Objects and Classes
10
Object references
Dog myDog = new Dog();
Remember: References are not objects!
i hc Cng ngh - HQG HN
Objects and Classes
11
Messaging between objects
Sending a message to an object is actually calling a
method of the object.
d.bark()
Syntax:
<object reference>.<method_name>(<arguments>)
recipient
i hc Cng ngh - HQG HN
message content
Objects and Classes
extra information
12
Methods How objects behave
Objects have
state (instance variables)
behavior (methods)
A method can use instance variables' value and
change the object's state.
A method can use instance variables so that objects
of the same type can behave differently
i hc Cng ngh - HQG HN
Objects and Classes
13
State affects behavior, behavior affects state
class Dog {
1. Write the class
int size;
String breed;
String name;
DOG
State affects behavior.
Dogs of different sizes
behave differently.
void bark() {
if (size > 14)
System.out.println("Ruff! Ruff!");
else
System.out.println("Yip! Yip!");
}
size
breed
name
bark()
getBigger()
method changes
state
void getBigger() {
size += 5;
}
}
i hc Cng ngh - HQG HN
Objects and Classes
14
State affects behavior, behavior affects state
class DogTestDrive {
1. Write the class
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
one.bark();
}
}
i hc Cng ngh - HQG HN
Objects and Classes
15
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
name:null
size:0
breed:null
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
%> java DogTestDrive
one.bark();
}
}
i hc Cng ngh - HQG HN
Objects and Classes
16
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
name:null
size: 7
breed:null
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
%> java DogTestDrive
one.bark();
}
}
i hc Cng ngh - HQG HN
Objects and Classes
17
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
name:null
size: 7
breed:null
Dog object 2
two
name:null
size:13
breed:null
%> java DogTestDrive
one.bark();
}
}
i hc Cng ngh - HQG HN
Objects and Classes
18
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
name:null
size: 7
breed:null
Dog object 2
two
name:null
size:13
breed:null
%> java DogTestDrive
Yip! Yip!
one.bark();
}
}
i hc Cng ngh - HQG HN
Objects and Classes
19
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
name:null
size: 7
breed:null
Dog object 2
two
name:null
size:18
breed:null
%> java DogTestDrive
Yip! Yip!
one.bark();
}
}
i hc Cng ngh - HQG HN
Objects and Classes
20
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
name:null
size: 7
breed:null
Dog object 2
two
name:null
size:18
breed:null
%> java DogTestDrive
Yip! Yip!
one.bark();
}
Ruff! Ruff!
i hc Cng ngh - HQG HN
Objects and Classes
21
State affects behavior, behavior affects state
Dog object 1
class DogTestDrive {
one
1. Write the class
public static void main (String[] args) {
Dog one = new Dog();
one.size = 7;
Dog two = new Dog();
two.size = 13;
two.bark();
two.getBigger();
two.bark () ;
name:null
size: 7
breed:null
Dog object 2
two
name:null
size:18
breed:null
%> java DogTestDrive
Yip! Yip!
one.bark();
}
Ruff! Ruff!
Yip! Yip!
%>
i hc Cng ngh - HQG HN
Objects and Classes
22
Compare
size, name
vs. bruno,
value
syntax?
scope?
class Dog {
int size;
String name;
...
void getBigger() {
size += 5;
}
}
i hc Cng ngh - HQG HN
public class DogTestDrive {
public static void main(String [] arg
Dog bruno = new Dog();
bruno.name = "Bruno";
...
int value = bruno.size;
}
}
Objects and Classes
23
Instance variables vs. local variables
Instance variables
belong to an object
declared inside a class but
NOT within a method
have default values (0, 0.0,
false, null)
class Dog {
int size;
String name;
...
void getBigger() {
size += 5;
}
}
i hc Cng ngh - HQG HN
Local variables
belong to a method
declared within a method
MUST be initialized before
use
public class DogTestDrive {
public static void main(String []
Dog bruno = new Dog();
bruno.name = "Bruno";
...
int size = bruno.size;
}
}
Objects and Classes
24
Encapsulation
Bad
class Person {
String name;
Date birthday;
String address;
// about his/her dog
String dogName;
String dogBreed;
int dogSize;
Better
class Dog {
int size;
String breed;
String name;
...
class Person {
}
String name;
Date birthday;
String address;
Dog petDog;
}
i hc Cng ngh - HQG HN
Objects and Classes
25
Encapsulation / information hiding
class Dog {
int size;
String breed;
String name;
...
Dog d = }new Dog();
d.size = -1;
What is wrong with this code?
It allows for
a supernatural dog
Object's data is exposed.
Exposed instance variables can lead to invalid states of object
What to do about it?
write set methods (setters) for instance variables
hide the instance variables to force other code to use the
set methods instead of accessing them directly.
i hc Cng ngh - HQG HN
Objects and Classes
26
Information hiding. Rule of thumb
Mark instance variables private.
Make getters and setters and mark them public.
class Dog {
private int size;
Don't forget to check
data validity in setters.
public void setSize(int s) {
if (s > 0) size = s;
}
public int getSize() {
return size;
}
...
i hc Cng ngh - HQG HN
Objects and Classes
27
Class access control
Access modifiers:
public : Accessible anywhere by anyone
private : Only accessible within the current
class
protected : Accessible only to the class itself
and to its subclasses or other classes in the same
package
default (no keyword): accessible within the current
package
i hc Cng ngh - HQG HN
Objects and Classes
28
Implementation vs. Interface
DogTestDrive: a client of Dog
Implementation
Data structures and code that
implement the object features
(instant variables and methods)
Usually more involved and may have
complex inner workings
Clients dont need to know
Interface
The controls exposed to the client by the implementation
The knobs on the black box
i hc Cng ngh - HQG HN
Objects and Classes
29
Encapsulation / information hiding
Dont expose internal data structures!
Objects hold data and code
Neither is exposed to
the end user or "client" modules.
Interface vs. implementation
A cat's look vs. its internal organs
A TV's screen & buttons vs. the stuff inside the box
Complexity is hidden inside the object
Make life easier for clients
More modular approach
Implementation changes in one component doesnt affect others
Less error-prone
i hc Cng ngh - HQG HN
Objects and Classes
30