2004-3-21 Sunday (Sunday)
Cloning method If this class can be cloned, then the next step is Clone method: ); obj.packets = (int []) packets.clone (); obj.size = size; return obj; } catch (CloneNotSupportedException e) { throw new InternalError ( "Unexpected CloneNotSUpportedException : " E.getMessage ()); String Retval = "Counterset:"; for (int i = 0; i Note three items in CloneDemo1. First, the throws CloneNotSupportedException clause attached to main () 's signature means you wish to ignore any CloneNotSupportedException objects thrown from clone (). Those exception objects are thrown in one of two situations -. either you overrode clone () and purposely threw the exception in a subclass (to prevent subclass objects from being cloned) or you called Object's clone () method from code in a class that does not also implement the Cloneable interface As you can probably guess, the second item to note is the implements Cloneable clause Cloneable is an interface -. a concept I will discuss in next month's article For now, keep in mind that an interface resembles a class, but can only. declare method signatures and constants. If you examine the documentation on Cloneable, you discover that the interface is empty. you must specify implements Cloneable in the class declaration for those classes whose code calls Object's clone () method. Behind the scenes, Cloneable helps Object's clone () method identify those subclasses that must have their field values duplicated. (Object's clone () method throws a CloneNotSupportedException object if a class, whose code calls that method, does not implement Cloneable.) THE Final Item to Note Is The Comment Line in CloneDemo1. That Line Attempts To Clone An Anotherclass Object. I I I ITED That Comment Because The Line of Code Won ' To Do So, You Must First Override Clone (), As Listing 6 DemonStrates: except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName, as seen in main () OuterClass oc = new OuterClass ();. OuterClass provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation. In addition, extension of an interface is useless from the client programmer's perspective since the client programmer can not access any additional methods that are not Part of the public interface. this also provides an Opportunity for the java compiler to generate more effect code code. -INNER) CLASS CANNOT BE MADE Private or protected they may only be given public or package access. If you're defining an anonymous inner class and want to use an object that's defined outside the anonymous inner class, the compiler requires that the argument reference be final, like the argument To DEST (). If you forget, You'll Get A Compile-Time Error Message. Feedback This case class, it is never directly used inside the anonymous class FeedbackHere's the "parcel" theme with instance initialization Note that the arguments to dest () must be final since they are used within the anonymous class:.. The inner class must keep a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, that (hidden) reference is used to select that member. Fortunately, the compiler takes care of all these details for you, but you can also understand now that an object of an inner class can be created only in association with an object of the enclosing class. Construction of the inner class object requires the reference to the object of the enclosing class, and the compiler will complain if it can not access that reference. Most of the time this occurs without any intervention on the part of the programmer.If you do not need a connection between the inner class object and the outer class object, then you can make the inner class static. This is commonly called a nested class. [36] to understand the meaning of static when applied to inner classes, you must remember that the object of an ordinary inner cla Ss Implicitly Keeps a Reference To The Object of The Enclosing Class That Created It. This is not true, however, when you say an inner class is static. a nested class means: feedback Outer-class Object in Order to create an Object of a nested class. feedback nested class (a static inner class), then it does not need a reference to the outer class objectEach inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer Class is already inheriting from an importation. ?