1 / 17

Chapter 8 Classes and Objects

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,

eadesm
Download Presentation

Chapter 8 Classes and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 Classes and Objects • Passing parameters to methods • The Object Class • Inner Classes

  2. Passing parameters to method Java uses exactly one mode of passing parameters: pass by value. • 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.

  3. Example: Test passing object public 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--; } } }

  4. 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

  5. Example Review main() printRadii() times Pass by value n 3 3 value is 3 c myC value is reference for the object myC : Circle radius = 1

  6. Passing parameters to method • 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. • Parameter value of a reference 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 method through variable myC.

  7. The Object Class The Object class is the root of all Java classes. • The booleanequals() method compares thecontents of two objects. • The String toString() method returns a string representation of the object. • The clone() method copy objects.

  8. 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); }

  9. 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()); } }

  10. 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. Example Circle c = new Circle(); System.output.println(c.toString()); // Circle@1de167

  11. The toString() method, cont. public class Circle extends Shape { ... public String toString() { return "[Circle] radius = " + radius; } } Example Circle c = new Circle(); System.out.println(c.toString()); // [Circle] radius = 1.0

  12. 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.Cloneableinterface (more later).

  13. 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.

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

  15. 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. • 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.

  16. 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.

  17. Inner Class: Example public 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(); // } } }

More Related