bpj444: business programming using java classes and objects tim mckenna seneca@york
TRANSCRIPT
BPJ444: Business Programming Using JavaClasses and Objects
Tim McKenna
Seneca@York
Outline
Classes and Objects in Java Method Overloading The Lifetime of an Object Access Control
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
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
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
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
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
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;}
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
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
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
Access Control Levels
access class subclasspackageworldmodifier
private x default x x
protected x x x
public x x x x