Chapter 8 Classes and Objects FPassing parameters to

Chapter 8 Classes and Objects FPassing parameters to methods FThe Object Class FInner Classes

Passing parameters to method Java uses exactly one mode of passing parameters: pass by value. F F When passing by value of a primitive data type, the value of the actual parameter is passed. When passing a parameter of a reference type, the reference of the object is passed.
![Example: Test passing object public class Test. Passing. Object() { public static void main(String[] Example: Test passing object public class Test. Passing. Object() { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/60a4be8dc0c93871501582c3eb75d273/image-3.jpg)
Example: Test passing object public class Test. Passing. Object() { public static void main(String[] args) { Circle new my. C = Circle(); // default int n = 3; print. Radii(my. C, n); System. out. println(“main(): radius is ” + my. C. get. Radius()); System. out. println(“main(): n is ” + n); } public static void print. Radii(Circle c, int times) { while(times >= 1) { System. out. println(“radius is ”+c. get. Radius()); c. set. Radius(c. get. Radius()+1); times--; } }

Example: Output. . . print. Radii(my. C, n); System. out. println(“main(): radius is ”+my. C. get. Radius()); 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 main() n print. Radii() times 3 3 Pass by value is 3 c value is my. C reference for the object my. C : Circle radius = 1

Passing parameters to method F Parameter value of a primitive 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. F Parameter value of a reference type (object or array): Parameter c contains a reference for the object that is also referenced via my. C. Therefore, changing properties of the object through c inside print. Radii() method has the same effiect as doing so outside the method through variable my. C.

The Object Class The Object class is the root of all Java classes. F The boolean equals() method compares the contents of two objects. F The String to. String() method returns a string representation of the object. F The clone() method copy objects.

The equals() method compares the contents of two objects. F The default implementation of the equals method in the Object class is as follows: F 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. get. Radius()); } } Example 2: public class Rectangle { public boolean equals(Rectangle r) { return (this. width == r. get. Width() && this. length == r. get. Length()); } }

The to. String() method F The String to. String() 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. Example F Circle c = new Circle(); System. output. println(c. to. String()); // Circle@1 de 167

The to. String() method, cont. public class Circle extends Shape {. . . public String to. String() { return "[Circle] radius = " + radius; } } Example Circle c = new Circle(); System. out. println(c. to. String()); // [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: new. Object = some. Object. 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 F The clone() method in the Object class copies each field from the original object to the target object. F If the filed is of primitive type, its value is copied. F If the field is of an object, the reference of the field is copied. F This is referred as shallow copy.

Deep copy Shallow copying is potentially dangerous action, it can cause side effects. F If you want to perform a deep copy, you can override the clone() method with custom cloning operations instead of invoking super. clone(); F

Inner Classes Inner class: A class is a member of another class. Advantages: In some applications, you can use an inner class to make programs simple. F 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. F But private members of inner class ae not visible for Inner class. F Inner class cannot be public.

Inner Classes (cont. ) F An inner (or nested) class is only for supporting the work of its containing outer class, and it cannot be used by other classes. F Inner classes can make programs simple and concise. F Many Java development tools use inner classes to generate adapters for handling events.

Inner Class: Example public class A { private int data; public void method() { // Do something Inner. Class a = new Inner. Class(); a. inner. Method(); } // An inner class Inner. Class { public void inner. Method() { // Directly reference field b and method // in its outer class data++; method(); // } } }
- Slides: 17