30
Classes Modeling the Object

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

Embed Size (px)

Citation preview

Page 1: 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

Classes

Modeling the Object

Page 2: 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

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 Fields – which describe what the class is Methods – which describe what the class does

Each instance of a Class is an object.

Page 3: 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

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

Page 4: 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

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.

Page 5: 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

Example

Class Car {

String LicensePlate; // “Ill 534 343”

double speed; // in miles per hour

double maxSpeed; // in miles per hour

public void stopCar(){

speed = 0;

}

}

Page 6: 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

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();

Page 7: 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

Member access

Accessing the members/fields of an object is done with the dot separatormycar.speedmycar.speed = 50;mycar.stopCar();

Page 8: 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

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;

}

}

Page 9: 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

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”);

}

}

Page 10: 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

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

Page 11: 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

Methods

Each method in Java has a signature similar to other languages

Access modifier Return type Name of method Any parameters/ arguments

Page 12: 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

Class Car {String LicensePlate =“”; // “Ill 534 343”double speed = 0; // in miles per hourdouble maxSpeed = 98.6; // in miles per hour

public void stopCar(){

this.speed = 0;} public void floorIt(){

this.speed = this.maxSpeed;}

Page 13: 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

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.

Page 14: 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

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;

}

}

Page 15: 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

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() {}

Page 16: 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

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 () {}

Page 17: 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

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.

Page 18: 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

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.

Page 19: 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

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 : 125if(this.speed >this maxSpeed){

this.speed = this.maxSpeed;}if(this.speed < 0.0){this.speed = 0.0;

} }

Page 20: 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

Access Protection

Four levelsPublicPrivateProtectedDefault aka package

Page 21: 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

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

Page 22: 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

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.

Page 23: 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

OK, what is what

General guidelines Classes public Fields private Constructors public Getters and setter public Other methods?? Decide as appropriate

case by case

Page 24: 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

Constructors in Java

Page 25: 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

Basic Syntax for constructors

[Access modifier] <class name> (any arguments)

{

<local variable declarations>

<nested local class declarations>

<statements>

}

Page 26: 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

Constructor Restrictions

ModifiersPublic or package for Top level classes

No return type Name MUST match class name

Page 27: 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

Default constructor

The zero argument constructor If no constructors are defined, java creates

thisThe only action by the implicit constructor is to

call the superclass constructor insuring that the inherited state is initialized.

Page 28: 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

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.

Page 29: 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

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.

Page 30: 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

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; }