Transcript Classes

Classes
Modeling the Object
Objects model the world



Classes are programmer defined types that
model the parts of a system
Class serve as blueprints for instances of the
class
Classes have two parts
– which describe what the class is
 Methods – which describe what the class does
 Fields

Each instance of a Class is an object.
Java OOP
In Java everything is an object or class
 Objects send messages to teach other by
method calls
 Instance methods belong to a particular
object
 Static methods belong to a particular class

Different objects of a class have the same
fields and methods.
 The values in the fields are different for
each object.

 Each

Person has eye color as a field
Each instance of a Person would have its own
value or color.
Example
Class Car {
String LicensePlate;
double speed;
double maxSpeed;
public void stopCar(){
speed = 0;
}
}
// “Ill 534 343”
// in miles per hour
// in miles per hour
Constructing an Object
Instantiating an object in Java is done with
the keyword new followed by the call to
the class’s constructor.
 Car mycar ;
declaration
 mycar = new Car(); initialization
 Car mycar = new Car();

Member access

Accessing the members/fields of an object
is done with the dot separator
 mycar.speed
 mycar.speed
= 50;
 mycar.stopCar();
Objects from a different class
Class CarTest {
public static void main(String[] args){
Car c = new Car();
c.speed = 55;
c.LicensePlate = “Il 564 586”
c.maxSpeed = 94.5;
}
}
Initializing Fields
Fields should be initialized when they are declared.
class Car {
String licensePlate = “”;
double speed = 0;
double maxSpeed =0;
String toString(){
System.out.println(licensePlate + ” is moving “ +
speed + “ miles per hour”);
}
}
class CarTest2{
public static void main(String[] args){
Car c = new Car();
c.toString();
}
}
javac Car.java
javac CarTest2.java
jara CarTest2
is moving at 0.0 miles per hour
Methods
Each method in Java has a signature
similar to other languages
 Access modifier
 Return type
 Name of method
 Any parameters/ arguments

Class Car {
String LicensePlate =“”; // “Ill 534 343”
double speed = 0;
// in miles per hour
double maxSpeed = 98.6;
// in miles per hour
public void stopCar(){
this.speed = 0;
}
public void floorIt(){
this.speed = this.maxSpeed;
}
Implied this
The “this” reference is not always used
 Each object has it’s own data and
methods and this instance would know
what its data is.
 This reference can also be used to
distinguish between local and instance
variables.

Passing Arguments
Altering the fields of an object is usually done by method calls, not
direct access to the field. This protects the data.
public void accelerate(double speed){
this.speed = this.speed + speed;
if(this.speed >this maxSpeed){
this.speed = this.maxSpeed;
}
if(this.speed < 0.0){
this.speed = 0.0;
}
}
Setters and Getters
Accessing data and mutating it
 Setter/ Mutator methods merely set the
value of a field to the value specified in the
argument.
 public void setSpeed(double speed) {}
 Accesors/ getters retrieve data
 Public double getSpeed() {}

Constructors

A constructor is required in order to
instantiate an object.
 If
you don’t write one, the compiler will.
Constructors have no return type, and the
method name is the same as the class
type.
 public Car () {}

Constructor function
Besides instantiating an object of the class
type
 Constructors initialize the data fields.
 If you don’t provide a value, all values are
set to defaults.

Default and Alternate
Constructors
You can build a constructor with any
number of arguments as a way of getting
data into your objects.
 public Car(String license, double speed,
double max) {}
 If you write an alternate,,, and you want a
zero argument constructor you must write
it.

Setting constraints.

Setting up constructor and setter methods are the
best way to control your data.
Public Car(double speed, double max){
this.speed = speed;
maxSpeed =(max !> 125 ) ? max : 125
if(this.speed >this maxSpeed){
this.speed = this.maxSpeed;
}
if(this.speed < 0.0){
this.speed = 0.0;
}
}
Access Protection

Four levels
 Public
 Private
 Protected
 Default
aka package
Public - Open access from anywhere or
any other object
 Private – the fields of an object or any
object of the same class (siblings)
 Protected – subclasses and classes in
same package
 Default – classes in same package

Benefits of Access Protection
Allows enforcement of constraints
 Provides simpler client interface. Clients
do not need to know everything, only the
public parts
 Separates interface from implementation,
allowing them to vary independently.

OK, what is what
General guidelines
 Classes public
 Fields private
 Constructors public
 Getters and setter public
 Other methods?? Decide as appropriate
case by case

Constructors in Java
Basic Syntax for constructors
[Access modifier] <class name> (any arguments)
{
<local variable declarations>
<nested local class declarations>
<statements>
}
Constructor Restrictions

Modifiers
 Public
or package for Top level classes
No return type
 Name MUST match class name

Default constructor

The zero argument constructor
 If
no constructors are defined, java creates
this
 The only action by the implicit constructor is to
call the superclass constructor insuring that
the inherited state is initialized.
Overloaded Constructors
Differ from default in arguments
 The first line of every constructor must be
either
 A this call to another constructor in the
same class.
 A super call to a parent constructor.

Why you might want to call super
explicitly



Normally, you won't need to call the constructor
for your parent class because it's automatically
generated, but there are two cases where this is
necessary.
You want to call a parent constructor which has
parameters (the automatically generated super
constructor call has no parameters).
There is no parameterless parent constructor
because only constructors with parameters are
defined in the parent class.
Static Initializer Blocks
It doesn't have a name, arguments, or
any access modifiers, and does not
return a value.

All we need to declare a static initializer
is the keyword "static" and the code
enclosed in braces.
static { Pi = 3.1415; }
