Classes and Objects in Java - Addendum: Parameter Passing, Delegation, Visibility Control, and Object Cleanup
Classes and Objects in Java - Addendum: Parameter Passing, Delegation, Visibility Control, and Object Cleanup
Contents
Parameter passing
Method parameters which are objects are passed by reference. Copy of the reference to the object is passed into method, original value unchanged.
Output
c1 Radius = 20.0 c2 Radius = 10.0 circleA Radius = 15.0 circleB Radius = 100.0 c1 Radius = 15.0 c2 Radius = 10.0
6
c1
c2
c1
c2
circleA
(5,5,20)
circleB
(1.1,10)
circleA
(5,5,20)
circleB
(1.1,10)
STEP3 circlA.setRadius(15) c1 c2
circleA
(5,5,15)
circleB
(1.1,10)
circleA
(5,5,15)
circleB
(1.1,10)
(0.0,100)
circleA X
(5,5,15)
circleB
(1.1,10)
Delegation
Ability for a class to delegate its responsibilities to another class. A way of making an object invoking services of other objects through containership.
Delegation - Example
public class Point { private double xCoord; private double yCoord; // Constructor .
public double getXCoord(){ return xCoord; } public double getYCoord(){ return yCoord; }
}
10
Delegation - Example
public class Circle { private Point centre; public double getCentreX(){ return centre.getXCoord(); } public double getCentreY(){ return centre.getYCoord(); } Circle }
centre getCentreX() getCentreY()
11
Java provides control over the visibility of variables and methods, encapsulation, safely sealing data within the capsule of the class Prevents programmers from relying on details of class implementation, so you can update without worry Helps in protecting against accidental or wrong usage. Keeps code elegant and clean (easier to maintain)
12
Default (No visibility modifier is specified): it behaves like public in its package and private in other packages.
available/visible everywhere. Applied to a method or variable, completely visible. Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited. Protected members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class.
13
Visibility
public class Circle { private double x,y,r; // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; } //Methods to return circumference and area public double circumference() { return 2*3.14*r;} public double area() { return 3.14 * r * r; } }
14
Visibility
Circle
Construction time message
circumference
message
15
Accessors Getters/Setters
public class Circle { private double x,y,r; //Methods to return circumference and area public double getX() { return x;} public double getY() { return y;} public double getR() { return r;} public double setX(double x) { this.x = x;} public double serY(double y) { this.y = y;} public double setR(double r) { this.r = r;} }
More on Visibility during Inheritance and Package Discussion
16
Objects Cleanup/Destructor
Unlike c and c++, memory deallocation is automatic in java, dont worry about it no dangling pointers and no memory leak problem. Java allows you to define finalizer method, which is invoked (if defined) just before the object destruction. In way, this presents an opportunity to perform recordmaintenance operation or cleanup any special allocations made by the user.
// done with this circle protected void finalize() throws IOException { Circle.numCircles = Circle.numCircles--; System.out.println(number of circles:+ Circle.num_circles); }
17
Summary
Objects can be passed as parameters and they can be used for exchanging messages (data). Delegation enables an object to pass responsibilities to other objects. Encapsulation/Data hiding helps in protecting data from accidental or wrong usage and also offers better security for data. Java clean-ups object resources automatically, however, users can provide finalize() method to do any user-level related clean-up activities.
18