Transcript ppt - ALI

ECE122
Feb. 17. 2005
Introduction to Class and Object
• Java is an object-oriented language.
• Java’s view of the world is a collection of
objects and their interactions.
• Class is a template, or blueprint to build
objects.
• Objects are instantiated (“created”) from the
class specification.
What is a class?
• A class is a template that defines the form of an object. It
specifies both the data and the code that will operate on
that data.
• A class is a set of plans that specify how to build an object.
• A class is a logical abstraction. It is a blueprint that resides
in the the Java class file.
• It has data: variables with certain data types. Some data are
associated with each individual object, called instance
variables. Other data are associated with each class, called
class variables, which are identified by keyword “static”.
• It has methods: the actions that act on those data. The
methods also have two types. Instance methods are
associated with each object, and class methods (identified
as “static”) are associated with the whole class.
General Forms of class definition
class classname
{
//declare variables
type var1;
type var2;
….
//declare methods
type method(parameters)
{
//body of method, consists of statements
}
type method2(parameters)
{
//body of method, consists of statements
}
….
}
A class example
public class Vehicle
{
//declare variables
int passengers; //number of passengers
int fuelcap; //fuel capacity in gallons
int mpg; //fuel consumption in miles per gallon
//omitted method declaration
}
What is an object?
• An object is an instantiation of a class.
• According to blueprints specified in the
class, an object has data (variables) and
actions (methods) that act on those data.
• One or more object of the same class can be
created in computer memory according to
their blueprint, class.
How is an object created?
• Objects are created through operator “new”.
Vehicle minivan; //declare reference to object
minivan = new Vehicle(); //construct a Vehicle object in memory
• Step 1: declare a variable called minivan of the class type
Vehicle. This is just a variable that can refer to an object.
At the point, minivan contains value “null”, meaning it is
not referring to any object yet.
• Step2: Allocate memory in computer and construct an
object of type “Vehicle” in that memory segment. This is
done by using operator “new”. Then, assign to “minivan” a
reference to that object.
How do I access an object’s data?
• Object contains data that you can access with operator “.”
in the form of
object.variable
for instance variable
e.g.
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
Or
e.g.
class.variable
for class variable
boolean b = Vehicle.hasEngine;
• Use variable directly without operator “.” if you reference
object data within a method of same object.
Primitive variable and assignment
• Assignment of primitive type is a straightforward copy of value from one variable to
another. E.g.
int i = 1;
int j = i;//the value contained in I is copied to j
Reference Variable and Assignment
• When you assign one reference variable to another
reference variable, the second variable begins to
reference the object the first reference variable is
referencing to.
• v = minivan; //both v & minivan are objects of Vehicle class
After this assignment, object v will reference the memory that object minivan
references to.
What happens to the memory block the v referenced to before the assignment?
It depends. If there are other object variables reference it. That memory block
stays untouched. If no more object variables reference it, it is no longer usable,
and become a “garbage”. It will wait for the garbage collector to free the
memory and recycle it back to system resource.
Illustration of Reference Variable
Assignment
v
minivan
Memory
block
v=minivan;
v
minivan
Garbage
collected
Methods
• Methods are subroutines that act on object’s data.
A method can call other methods of its own class,
it can also call methods of other classes.
• Object interacts with each other through method
calls.
• You can model the real world through object
interactions.
• Instance method is created and associated with
each object created.
• Class method is created when the class is loaded
and class method is shared among all the objects
of the same class.
General Form of a Method
• ReturnType MethodName(parameter-list)
{
//body of method
}
• MethodName is the name of the method as a string with no space.
Capitalize first letter of each word as a convention.
• ReturnType is any primitive type, or object reference. If nothing is
returned from the method to the method-caller, a “void” is declared.
• Parameter-list is a comma separated list of type and variable pair. Each
pair pass a value of certain data type to the method.
• The body of a method consists of statements.
Add methods to Vehicle Class
Add an instance method to Vehicle class:
void range()
{
System.out.println("Range is " + fuelcap*mpg + " miles.");
}
Add a class method to Vehicle class:
static int calcRange(int fuelCap, int mpg)
{
int theRange = fuelCap * mpg;
return theRange;
}
Call a method
• Call an instance method
objectReference.methodName(parameterlist);
minivan.range();
• Call a class method
ClassName.methodName(parameterlist);
Vehicle.calcRange(18,33);
• If the caller of the method is another
method of the same object, just use the
methodName, without operator dot.
Java methods pass by value
• A java method can use parameters.
• A method caller can passes arguments.
• The data type of argument normally is the same as that of
the parameter. Otherwise, an explicit/implicit cast will
happen.
• Java pass everything (primitive type & reference type) by
VALUE. For primitive type, this means copy of value. For
reference type, this means you cannot change the object
the original argument variable reference to, but you can
change the data contains within the original object.
Assignments
• Practice Vehicle.java
• Read “Java2” P115-138
• Read “Head First Java” p26 – 29, 32-37,5257, 69-77