4 DefiningClasses PartA
4 DefiningClasses PartA
Introduction to Programming
Chapter 4 - Defining Classes
Part A
These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch;
which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by
Pearson Education / Addison-Wesley.
A class determines:
1) Attributes, or Instance Variables: the types of data that an object can
contain,
2) Methods: the actions it can perform
4-2
Class Definitions
int x, y;
Data declarations
char ch;
Method declarations
4-3
The new Operator
An object of a class is named or declared by a variable of the class
type:
ClassName objectName;
The new operator must then be used to create the object and
associate it with its variable name (however, some few exceptions do
exist):
objectName = new ClassName();
Example:
Car c1 = new Car(); // Car is the class name and
// c1 is the object name
// IMPORTANT NOTE: In fact,
// c1 is a pointer/reference
// to the object
4-4
Instance Variables and Methods
Instance variables (attributes) can be defined as in the
following two examples
Note the public modifier (for now):
public int numberOfDoors;
public double Price;
Methods are invoked using the name of the calling object and
the method name as follows:
objName.methodName();
Example:
C1.getNumberOfDoors();
4-7
More About Methods
There are two kinds of methods:
Methods that compute/perform an action then
return a value
Methods that compute/perform an action then does
not return a value
This type of method is called a void method; in other
words, it returns void
Examples:
public double getPrice();
public int getNumOfDoors();
public void setNumOfDoors(int nd); // nd is just
// a name
4-9
main is a void Method
A program in Java is just a class that has a main
method
4-10
return Statements
The body of both types of methods contains a list of
declarations and statements enclosed in a pair of braces
public <void or typeReturned> myMethod()
{
declarations Body
statements
}
4-11
return Statements
The body of a method that returns a value must
also contain one or more return statements
4-13
Method Definitions
An invocation of a method that returns a value can be
used as an expression anyplace that a value of the
returned type can be used:
double pr;
pr = c1.getPrice();
Examples:
c1.setPrice(20000);
c1.showModel();
v3
numOfDoors
4
price
10000
maxSpeed
280
v1, v2 & v3 upon creation
4-15
Constructors
A constructor is a special kind of method that is designed
to initialize the instance variables for an object:
public ClassName(anyParameters){code}
4-16
public and private Modifiers
The modifier public means that there are no restrictions on
where an instance variable or method can be used
4-17
Include a No-Argument Constructor
You should include a default, or no-argument constructor as part of
your program. Default constructors will be discussed later in full
details.
4-18
Local Variables
A variable declared within a method definition is
called a local variable
All variables declared in the main method are local
variables
All method parameters are local variables
4-19
Global Variables
Some programming languages include another
kind of variable called a global variable
4-20
Blocks
A block is another name for a compound statement, that
is, a set of Java statements enclosed in braces,{}
4-21
Declaring Variables in a for Statement
You can declare one or more variables within the
initialization portion of a for statement
4-23
Parameters of a Primitive Type
The type of each argument must be compatible with the type of
the corresponding parameter. The following two statements use
the method correctly
c1.setVehicleInfo(4, 12500.99, 280);
int n = 5, m = 260;
double p = 19700.95;
c1.setVehicleInfo(n, p, m);
4-25
Methods That Return a Boolean Value
4-28
Encapsulation
4-29
A Class Has Access to Private Members of All
Objects of the Class
Within the definition of a class, private members
of any object of the class can be accessed, not
just private members of the calling object
4-30