BPJ444: Business Programming using Java Java basics Tim McKenna Seneca@York.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna Seneca@York.
-
Upload
mervyn-barnett -
Category
Documents
-
view
212 -
download
0
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.net/reader036/viewer/2022072013/56649e695503460f94b66d41/html5/thumbnails/12.jpg)
Access Control Levels
access class subclasspackageworldmodifier
private x default x x
protected x x x
public x x x x