Transcript Chapter3

Chapter 3
Introduction to Classes and Objects
Definitions
Examples
Definitions




Object – instance of a class
Objects have attributes and behaviors
Attributes – data
Behaviors – methods or operations
Definitions


Encapsulation – combining attributes and
methods into one group (object)
Information hiding
–

Implementation details are hidden within the
objects themselves
Abstraction – another term for information
hiding
Definition




Default package – set of classes that are compiled
in the same directory on disk.
We say that all such classes are considered to be in
the same package
Classes in the same package are implicitly imported
into the source code files of other classes in the
same package.
Import is not required when one class in a package
uses another class in the same package.
Definitions

Java classes contain
–
–

Methods – implement operations
Fields – implement attributes
Accessor methods
Methods that allows access to internal data fields
Example:
getInterestRate()
//method name generally starts with “get”
//highly recommended that it starts with “get”
–
Definitions
Mutator methods – methods that change
values in data fields
Example:

setInterestRate (double x)
//method name generally starts with “set”
//highly recommended that mutator methods start
//with “set”
Variables

Local variables
–

Variables declared in the body of a particular
method
Fields
–
–
Variables declared inside a class declaration but
outside the bodies of the class’s method
declarations
Also known as instance variables (if not static)
Fields – instance variables



Can be declared as public, private,
protected, default
Generally should be declared as private to
promote data hiding
Methods generally declared as public
Example class (Circle.java)
public class Circle
{
private double radius = 1.0;
//field
double findArea()
{
return radius * radius * 3.13159;
}
}
Declaring a reference to an object and
creating an object

Declaration: Classname objectReference;
Circle myCircle; //myCircle is a reference to a Circle
//object

Create a Circle object
myCircle = new Circle();

Declare and create
Circle myCircle = new Circle();
Circle Class


CircleClass.java
CircleProgram.java
Constructors




Method with the same name as the class
Allows construction of objects with different initial
data values – initializes object
Do not have a return type
Invoked using the new operator
Circle (double r)
Circle ()
{
{
radius = r;
radius = 1;
}
}
Constructors

Default constructor
–



Constructor with no parameters
If your class has no constructors, Java will
automatically create a default constructor
If your class has a constructor, Java will not
create a default constructor
You should include default constructors in
your class.
Instance Variables
Normally should be initialized in constructors
 Alternative: Initialize when declarations are made
 If instance variables are initialized in the declaration
and you have constructors --- Be sure to define a
default constructor even if the body of the
default constructor is empty.
The following:
AClass anObject = new AClass();
is illegal if no default constructor but other constructors
exist.

AClass anObject = new AClass();
public class AClass
{
private int value = 1;
public AClass (int x)
{
value = x;
}
public void setValue(int x)
{
value = x;
}
public int getValue()
{
return value;
}
Static Methods – methods that do not
require a calling object


Uses the keyword static in method heading
You cannot do anything inside a static method that
refers to a calling object
–


You cannot access instance variables from inside a static
method
You cannot call a non-static method inside a static
method )unless you first create a new object and use
it as the calling object)
You can invoke a static method with an object
–
Confusing and is not normally done.
Method Syntax
[access specifier]
[qualifier]
return type
method name
(argument list)
Access Specifiers – protecting class
variables and methods
Specifier Class
Private
Subclass Package World
X
Protected X
X
X
Public
X
X
X
Package
(default)
X
X
X
Static Variables

Static variables belong to the class as a
whole and not just to one object
–



private static int turn;
Used to communicate between objects
One object can change the static variable
and another object can read that change
Should normally be declared as private
(unless declared as constants)
Final Instance Variables


Keyword final is used to specify that a
variable is not modifiable (it is a constant)
private final int INCREMENT = 5;
Use all caps when denoting constants
Method arguments


All arguments are pass by value – methods
always receive a copy of the argument
If argument is an object reference (different
story)
Class methods – static methods

Example
–
–



Integer.parseInt(s);
main (String args[])
An instance of the class is not necessary to access
the static method
Static methods can only call static methods and can
only access static variables
Method is usable by any code that has access to the
method – regardless of whether any objects of the
class have been created.