![]() An object that comes from an inner class has an implicit reference to the outer class object that instantiated it. There are three reasons for inner classes:Ī) Inner class methods can access the data from the scope in which they are defined-including the data that would otherwise be private.ī) Inner classes can be hidden from other classes in the same package.Ĭ) Anonymous inner classes are handy when you want to define callbacks without writing a lot of code.ġ6. Consider using an interface in Java for callback function.ġ5. ![]() In this pattern, you want to specify the action that should occur whenever a particular event happens. A common pattern in programming is the callback pattern. ![]() You can use it to make a new array that contains copies of all elements (elements are not deeply copied).ġ4. All array types have a clone method that is public, not protected. There is no problem not to override the clone method in the subclass if superclass provides the implementation of clone if the fields of subclass are all primitive types.ġ3. Objects are so paranoid about cloning that they generate a checked exception ( CloneNotSupportedException) if an object requests cloning but does not implement that interface.ġ2. The Cloneable interface has no method and merely serves as a tag, indicating that the class designer understands the cloning process. A class must implement the Cloneable interface and redefine the clone method to call super.clone() and assign it the public access modifier to allow objects to be cloned by any method.ġ1. If subobjects are mutable, and you must redefine the clone method to make a deep copy that clones the subobjects as well. The default cloning operation is “shallow”.-it doesn’t clone objects that are referenced inside other objects.ġ0. But if the object contains references to subobjects, then copying the field gives you another reference to the same subobject, so the original and the cloned objects still share some information. If all data fields in the object are numbers or other basic types, copying the fields is just fine. It knows nothing about the object at all, so it can make only a field-by-field copy. Think about the way in which the Object class can implement clone. The rules for protected access make sure that a subclass can call a protected clone method only to clone its own objects.ĩ. The clone method is a protected method of Object, which means that your code cannot simply call it. Each class can have only one superclass but implement multiple interfaces.Ĩ. ![]() You can use instanceof to check whether an object implements an interface.ħ. If there is a common algorithm for comparing subclass objects, simply provide a single compareTo method in the superclass and declare it as final.Ħ. If (getClass() != other.getClass()) throw new ClassCastException() Each compareTo method should start out with the test: If subclasses have different notions of comparison, then you should outlaw comparison of objects that belong to different classes. That is, x.compareTo(y) should be zero exactly when x.equals(y).ĥ. This implies that x.compareTo(y) must throw an exception if y.compareTo(x) throws an exception. The documentation of the Comparable interface suggests that the compareTo method should be compatible with the equals method. If you flip the parameters of compareTo, the sign (but not necessarily the actual value) of the result must also flip. Subtraction of two floating-point numbers can round to 0 if they are close enough.Ĥ. Of course, the subtraction trick doesn’t work for floating-point numbers. If you know that the IDs are not negative or that their absolute value is at most (Integer.MAX_VALUE - 1) / 2, you are safe to use integer substraction in the compareTo method. pare method returns a negative if the first argument is less than the second argument, 0 if they are equal, and a positive value otherwise.ģ. Methods in an interface are automatically public, fields are always public static final. As of Java SE 8, you are allowed to add static methods to interfaces.Ģ. You cannot put instance fields or static methods in an interface (because you can't implement it and its implementation cann't override it due to static), while you can supply constants in them. Methods are never implemented in the interface.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |