BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

12
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York

Transcript of BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Page 1: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

BPJ444: Business Programming Using JavaClasses and Objects

Tim McKenna

Seneca@York

Page 2: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Outline

Classes and Objects in Java Method Overloading The Lifetime of an Object Access Control

Page 3: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Classes and Objects in Java

members of a class are: fields or variables

a class or object's state or attributes methods

a class or object's behaviour constructors

to make objects each has access control

Page 4: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Types of Members

fields or variables instance unique values for each object class uses the static modifier

same value for all objects methods

instance work with both instance and static variables

class uses static modifierworks with static

variables

Page 5: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Fields or Variables

field or variable holds a single value is strongly typed: must be declared type specifies the kind of value and the

variable's behaviour int i = 123; // integer primitive Point pt = null; // object reference pt = new Point (x, y); // coordinates

pt has the value of the Point object's address, not the x, y coordinates

Page 6: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Constructors

a special kind of method to construct an instance of an object from a class template

default constructor is ClassName() created if the class has no constructors

instance fields are initialized to default values automatically (see next slide)

overloaded constructors: same name with different parameter lists

use of “this” to call another constructor Example: Fruit.java

Page 7: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Java automatically initializesclass (static) and object (instance) fields

Primitive Data Type Default Valueboolean falseint, etc. 0

Object Reference Type null- not yet referring to an object

Page 8: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Constructor initializes an object's instance fields

class MyClass {int maximum;int counter;boolean isBelowMax = true; // override default

MyClass(int maximum) {this.maximum = maximum;

}

MyClass() { this(100); } // calls this class's constructor MyClass(int maximum, int counter) {

this.maximum = maximum; // assign local field valuethis.counter = counter; // to this object's instance

isBelowMax = counter < maximum;}

Page 9: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Methods

overloading is supported:same method name, different parameter lists

parameters passed by value, i.e. by copy primitive data types and object reference types copy of object reference is not a copy of the object

type of primitive/object returned or void Example: SwapDemo2.java

demonstrates parameter passing

Page 10: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

The Lifetime Of An Object

an object exists as long as it is referenced in some active method

an object can exist beyond the method in which it is instantiated as long as a reference to that object persists

automatic garbage collection:reclaims memory of unreferenced objects

Example: LifeTime.java

Page 11: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Four Levels of Access to a Class or object's members

private: accessible from within this class only should be standard practice on instance fields

to support OO encapsulation

default (if you don't specify): accessible from any class in the package

protected: accessible from any subclass or any class in the package

public: accessible from any class anywhere

Page 12: BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.

Access Control Levels

access class subclasspackageworldmodifier

private x default x x

protected x x x

public x x x x