Title: Java: Classes and Methods Part I
1Java Classes and Methods(Part I)
September 28, 2007
2Last Time
operations
operations methods
data
Java uses method
3Objects
- Object-oriented systems are collections of
interacting objects
state data
state data
state data
state data
4Objects
- Physically occupy space in memory
- Have state (data)
- Have behavior (operations)
aspect of behavior
5Objects State
- State
- What the object can remember
- Consists of individual elements
- Referred to as attributes
- Behavior
- What the object can do
- Individual aspects of behavior are called methods
6Class vs. Objects
objects instances of classes
classes abstract a family of objects
objects encapsulate state and behavior
classes definestate and behavior
7OO Terminology
8Operation vs. Method
- Operations are behaviors that can be invoked on
an object - Methods are what happens when an operation is
invoked
Two objects can have the same operation but
different methods! This is an important concept
in object-oriented programming. We will revisit
this later.
9Operations vs. Method
- The terms are often used synonymously
- We will only worry about the distinction when
necessary - Do note that there is a distinction and it is
important - Java uses the term method
10Classes in Java
11Class Declaration
public class Rectangle private int
height private int width public void
setHeight(int newHeight) height
newHeight public void setWidth(int
newWidth) width newWidth
public int getHeight() return height
public int getWidth() return
width public int getArea()
return heightwidth public int
getPerimeter() return 2
(heightwidth)
12Class Attributes
public class Rectangle private int
height private int width public void
setHeight(int newHeight) height
newHeight public void setWidth(int
newWidth) width newWidth
public int getHeight() return height
public int getWidth() return
width public int getArea()
return heightwidth public int
getPerimeter() return 2
(heightwidth)
public class Rectangle private int
height private int width public void
setHeight(int newHeight) height
newHeight public void setWidth(int
newWidth) width newWidth
public int getHeight() return height
public int getWidth() return
width public int getArea()
return heightwidth public int
getPerimeter() return 2
(heightwidth)
13Class Methods
public class Rectangle private int
height private int width public void
setHeight(int newHeight) height
newHeight public void setWidth(int
newWidth) width newWidth
public int getHeight() return height
public int getWidth() return
width public int getArea()
return heightwidth public int
getPerimeter() return 2
(heightwidth)
public class Rectangle private int
height private int width public void
setHeight(int newHeight) height
newHeight public void setWidth(int
newWidth) width newWidth
public int getHeight() return height
public int getWidth() return
width public int getArea()
return heightwidth public int
getPerimeter() return 2
(heightwidth)
14Using Classes
// Create an instance of a rectangle (well //
explain this shortly) and use it.//Rectangle r
new Rectangle()r.setWidth(6)r.setHeight(5)
System.out.println(A rectangle with width
r.getWidth()
and height
r.getHeight()
has area
r.getArea()
and perimeter
r.getPerimeter()
.)
15Using Classes
// Create an instance of a rectangle (well //
explain this shortly) and use it.//Rectangle r
new Rectangle()Rectangle s new
Rectangle()r.setWidth(6)r.setHeight(5)sr
s.setWidth(10)System.out.println(Width of
r r.width())System.out.
println(Width of s
s.width())
16Using Classes
- All objects of non-primitive types are reference
objects
// Create an instance of a rectangle (well //
explain this shortly) and use it.//Rectangle r
new Rectangle()Rectangle s new
Rectangle()r.setWidth(6)r.setHeight(5)sr
s.setWidth(10)System.out.println(Width of
r r.width())System.out.
println(Width of s
s.width())
// Create an instance of a rectangle (well //
explain this shortly) and use it.//Rectangle r
new Rectangle()Rectangle s new
Rectangle()r.setWidth(6)r.setHeight(5)sr
s.setWidth(10)System.out.println(Width of
r r.width())System.out.
println(Width of s
s.width())
Assignment of references not a deep copy
17Access Modifiers
- Attributes and methods can have different levels
of access - Indicates what other classes can access resources
- There are four levels in Java
- Public
- Private
- Protected
- Package (not an actual modifier)
18Access Modifiers
19Public vs Private
public class QuizScores private int
scores private int scoreCount
private boolean isSorted false private
void sortList() //Sort scores
public void addValue(int newScore)
isSorted false //Add a new value
to the array. public void min()
if (!isSorted) sortList()
isSorted true return scores0
public void min() if
(!isSorted) sortList()
isSorted true return
scores0
Attributes are generally private think
personal memoryPrivate methods are local
utility functionality and typically are not
defined for public consumption
QuizScores qs new QuizScores()qs.addValue(8
8) // OK. addValue() is publicqs.addValue(92)
if (qs.scoreCount gt 0) //compilation error
... // scoreCount is
// private
qs.sortList() // compilation
error // sortList() is
private
20Protected and Package
public class QuizScores int scores
int scoreCount boolean isSorted
false protected void sortList()
//Sort scores public void
addValue(int newScore) ...
public void min() ... public
void min() ...
public class QuizScores int scores
int scoreCount boolean isSorted
false protected void sortList()
//Sort scores public void
addValue(int newScore) ...
public void min() ... public
void min() ...
No access modifiers meanspackage visibility.
Protected visibility uses the protected
modifier
21Protected and Pacakge
- Pure object oriented approach
- Use only public and private
- Generally avoid package visibility
- Definitely is anti-OO
- Protected
- Has its utility
- Typically I avoid
22General Rule of Thumb
- Start by thinking of everything private
- Expose more only if necessary
- Attributes are (almost) always private
- This is called information hiding
- Makes for strong software
- Try to stick to public and private modifiers
- As you get better, introduce protected sparingly