04 CreatingClassAndObject
04 CreatingClassAndObject
1
Classes
2
UML Class Diagram for Employee
Class name
Employee
- empnum: int Attributes / instance variables that
- empname: String define the object’s state; can hold
numbers, characters, strings, other
+ setEmpNum(int): void objects
+ getEmpNum(): int
+ setEmpName(String): void
+ getEmpName(): String Actions that an object of this
class can take (behaviors)
3
The Employee Class Shell
4
Data Fields
Variables declared within class
But outside of any method
Called instance variables (local)
When not static (global)
Each class will have own copy of each
non-static data field
private access for fields
No other classes can access field’s values
Only methods of same class allowed to use
private variables
5
You name it!
Data Fields (cont’d)
private [static] [final] datatype name;
Usually
private primitive: int, double,
etc., or an object:
May be present: String, Image, Foot
means the field is
shared by all May be present:
objects in the class means the field
is a constant
7
7
Method Definitions
Method header form:
modifier returnType methodName ([parameterList])
Example:
public double getPrice()
public void setPrice(double aPrice)
Method names usually sound like verbs.
The name of a method that returns the value of a
field often starts with get:
getWidth, getX
The name of a method that sets the value of a
field often starts with set:
setLocation, setText
8
Understanding Data Hiding
Data hiding using encapsulation
Data fields usually private
9
Accessor and Mutator Methods
The methods that retrieve the data of
fields are called accessors.
The methods that modify the data of
fields are called mutators.
10
Accessors and Mutators
For the rectangle example, the accessors and
mutators are:
getLength: Returns the value of the length field.
public double getLength() { }
getWidth : Returns the value of the width field.
public double getWidth() { }
setLength : Sets the value of the length field.
public void setLength(double len) { }
setWidth : Sets the value of the width field.
public void setLength(double w) { }
11
Class Definition
public class Employee {
private int empnum;
private String empname;
Employee
public void setEmpNum(int num) {
- empnum: int empnum = num;
- empname: String }
+ setEmpNum(int): void public int getEmpNum() {
+ getEmpNum(): int return empnum;
+ setEmpName(String): void }
+ getEmpName(): String
public void setEmpName(Strig name) {
UML class diagram empname = name;
}
13
Declaring Object Reference Variable
and Create Object
To refer an object, assign the object to a
reference variable.
To declare a reference variable, use the syntax:
ClassName objectRefVar;
To create object:
objectRefVar = new ClassName();
Example:
Employee emp1;
emp1 = new Employee();
14
Declaring/Creating Objects
in a Single Step
Example:
Employee emp1 = new Employee();
15
Accessing Objects
After object instantiated, methods accessed using:
Object’s identifier
Dot
Data/Method call
19
Creating Object == invoking Constructors
20
Constructors (cont’d)
public class Fraction public Fraction (int n, int d)
{ {
private int num, denom; num = n;
denom = d;
public Fraction ( ) reduce ();
{ }
num = 0;
denom = 1; “No-args” public Fraction (Fraction other)
} constructor {
num = other.num;
public Fraction (int n) denom = other.denom;
{ }
num = n; ... Copy
denom = 1;
} Continued }
constructor
21
Constructors (cont’d)
A nasty bug:
public class MyWindow
extends JFrame
{
...
// Constructor: Compiles fine, but
public void MyWindow ( ) the compiler thinks
{ this is a method and
...
}
uses MyWindow’s
... default no-args
constructor instead.
22
Constructors (cont’d)
23
Organizing Classes
Place data fields in logical order
At beginning of class
Fields listed vertically
May place data fields and methods in any
order within a class
Common to list all data fields first
Can see names and data types before reading
methods that use data fields
24
Employee.java
26