Presentation is loading. Please wait.

Presentation is loading. Please wait.

BPJ444: Business Programming Using Java Classes and Objects Tim McKenna

Similar presentations


Presentation on theme: "BPJ444: Business Programming Using Java Classes and Objects Tim McKenna"— Presentation transcript:

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

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

3 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

4 Types of Members fields or variables  instanceunique values for each object  classuses the static modifier same value for all objects methods  instancework with both instance and static variables  classuses static modifier works with static variables

5 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

6 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

7 Java automatically initializes class (static) and object (instance) fields Primitive Data TypeDefault Value boolean false int, etc. 0 Object Reference Type null - not yet referring to an object

8 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 value this.counter = counter; // to this object's instance isBelowMax = counter < maximum; }

9 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

10 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

11 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

12 Access Control Levels accessclass subclasspackageworld modifier private x default x x protected x x x public x x x x


Download ppt "BPJ444: Business Programming Using Java Classes and Objects Tim McKenna"

Similar presentations


Ads by Google