Java: Classes and Methods Part I - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Java: Classes and Methods Part I

Description:

Collection of logically related data and functions. Operation ... The terms are often used synonymously. We will only worry about the distinction when necessary ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 23
Provided by: harold98
Category:

less

Transcript and Presenter's Notes

Title: Java: Classes and Methods Part I


1
Java Classes and Methods(Part I)
September 28, 2007
2
Last Time
operations
operations methods
data
Java uses method
3
Objects
  • Object-oriented systems are collections of
    interacting objects

state data
state data
state data
state data
4
Objects
  • Physically occupy space in memory
  • Have state (data)
  • Have behavior (operations)

aspect of behavior
5
Objects 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

6
Class vs. Objects
objects instances of classes
classes abstract a family of objects
objects encapsulate state and behavior
classes definestate and behavior
7
OO Terminology
  • A class is

8
Operation 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.
9
Operations 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

10
Classes in Java
11
Class 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)
12
Class 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)
13
Class 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)
14
Using 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()
.)
15
Using Classes
  • Whats the output?

// 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())
16
Using 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
17
Access 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)

18
Access Modifiers
19
Public 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
20
Protected 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
21
Protected 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

22
General 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
Write a Comment
User Comments (0)
About PowerShow.com