chapter 8 classes and objects
DESCRIPTION
Chapter 8 Classes and Objects. Passing parameters to methods The Object Class Inner Classes. Passing parameters to method. Java uses exactly one mode of passing parameters: pass by value . When passing by value of a primitive data type, - PowerPoint PPT PresentationTRANSCRIPT
Chapter 8 Classes and Objects
Passing parameters to methodsThe Object ClassInner Classes
Passing parameters to method
Java uses exactly one mode of passing parameters:pass by value.
When passing by value of a primitiveprimitive data type, the value of the actual parameter is passed.
When passing a parameter of a referencereference type,
the reference of the object is passed.
Example: Test passing objectpublic class TestPassingObject() { public static void main(String[] args) { Circle new myC = Circle(); // default int n = 3; printRadii(myC, n); System.out.println(“main(): radius is ” + myC.getRadius()); System.out.println(“main(): n is ” + n); }
public static void printRadii(Circle c, int times) { while(times >= 1) { System.out.println(“radius is ”+c.getRadius()); c.setRadius(c.getRadius()+1); times--; } }}
Example: Output ...
printRadii(myC, n);
System.out.println(“main(): radius is ”+myC.getRadius());
System.out.println(“main(): n is ” + n);
Output: radius is 1.0
radius is 2.0
radius is 3.0
main(): radius is 4.0
main(): n is 3
Example Review
3 3
main() printRadii()
times
Pass by value
n
myCc
value is
reference for the
object
myC : Circle
radius = 1
value is 3
Passing parameters to method Parameter value of a primitiveprimitive data type. The value of n (3) is passed to times. Inside the method,
the content of times is changed; it does not affect the
content of n.
Parameter value of a referencereference type (object or array):
Parameter c contains a reference for the object that is also
referenced via myC. Therefore, changing properties of
the object through c inside printRadii() method
has the same effiect as doing so outside the methodthrough variable myC.
The Object Class
The Object class is the root of all Java classes.
The boolean equals() method compares thecontents of two objects.
The String toString() method returns a string representation of the object.
The clone() method copy objects.
The equals() method The equals() method compares thecontents of two objects.
The default implementation of the equals method in the Object class is as follows:
public boolean equals(Object obj)
{ return (this == obj);}
The equals() method, cont.Example 1:
public class Circle {
public boolean equals(Circle c)
{
return (this.radius == c.getRadius());
}
}
Example 2:
public class Rectangle {
public boolean equals(Rectangle r) {
return (this.width == r.getWidth() &&
this.length == r.getLength());
}
}
The toString() method The String toString() method returns a
string representation of the object.
The default implementation returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object.
ExampleCircle c = new Circle();System.output.println(c.toString()); // Circle@1de167
The toString() method, cont.public class Circle extends Shape {...
public String toString() {
return "[Circle] radius = " + radius;
}}
ExampleCircle c = new Circle();System.out.println(c.toString()); // [Circle] radius = 1.0
The clone() method
To create a new object with separate memory space,
you need to use the clone() method, as follows:
newObject = someObject.clone();
NOTE: Not all objects can be cloned. For an object to be cloneable, its class must implement the java.lang.Cloneable interface (more later).
Shallow copy
The clone() method in the Object class copies each field from the original object to the target object. If the filed is of primitive type, its value is copied . If the field is of an object, the reference of the field is copied. This is referred as shallow copy.
Deep copy
Shallow copying is potentially dangerous action, it can cause side effects.
If you want to perform a deep copy, you canoverride the clone() method with custom cloning operations instead of invokingsuper.clone();
Inner ClassesInner class: A class is a member of another class.
Advantages: In some applications, you can use an inner class to make programs simple.
An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class.
But private members of inner class ae not visible for Inner class.
Inner class cannot be public.
Inner Classes (cont.) An inner (or nested) class is only for supporting
the work of its containing outer class, and it cannot be used by other classes.
Inner classes can make programs simple and concise.
Many Java development tools use inner classes to generate adapters for handling events.
Inner Class: Examplepublic class A { private int data; public void method() { // Do something InnerClass a = new InnerClass(); a.innerMethod(); } // An inner class class InnerClass { public void innerMethod() { // Directly reference field b and method // in its outer class data++; method(); // } } }