`
leonzhx
  • 浏览: 768160 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 6. Interfaces and Inner Classes -- Core Java Ninth Edition

阅读更多

1.  Methods are never implemented in the interface. 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 in an interface are automatically public, fields are always public static final.


2.  Double.compare method returns a negative if the first argument is less than the second argument, 0 if they are equal, and a positive value otherwise.

 

3.  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. Of course, the subtraction trick doesn’t work for floating-point numbers. Subtraction of two floating-point numbers can be 0 if they are close enough.

 

4.  If you flip the parameters of compareTo, the sign (but not necessarily the actual value) of the result must also flip. This implies that x.compareTo(y) must throw an exception if y.compareTo(x) throws an exception.

 

5.  If subclasses have different notions of comparison, then you should outlaw comparison of objects that belong to different classes. Each compareTo method should start out with the test:

if (getClass() != other.getClass()) throw new ClassCastException();

If there is a common algorithm for comparing subclass objects, simply provide a single compareTo method in the superclass and declare it as final.

 

6.  You can use instanceof to check whether an object implements an interface.

 

7.  Each class can have only one superclass but implement multiple interfaces.

 

8.  The clone method is a protected method of Object, which means that your code cannot simply call it. The rules for protected access make sure that a subclass can call a protected clone method only to clone its own objects.

 

9.  Think about the way in which the Object class can implement clone. It knows nothing about the object at all, so it can make only a field-by-field copy. If all data fields in the object are numbers or other basic types, copying the fields is just fine. 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. The default cloning operation is “shallow”.

 

10.  If subobjects are mutable, and you must redefine the clone method to make a deep copy that clones the subobjects as well. 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.

 

11.  The Cloneable interface merely serves as a tag, indicating that the class designer understands the cloning process. Objects are so paranoid about cloning that they generate a checked exception (CloneNotSupportedException) if an object requests cloning but does not implement that interface.

 

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

 

13.  All array types have a clone method that is public, not protected. You can use it to make a new array that contains copies of all elements (elements are not deeply copied).

 

14.  A common pattern in programming is the callback pattern. In this pattern, you want to specify the action that should occur whenever a particular event happens. Consider using an interface in Java for callback function.

 

15.  There are three reasons for inner classes:
    a)  Inner class methods can access the data from the scope in which they are defined—including the data that would otherwise be private.
    b)  Inner classes can be hidden from other classes in the same package.
    c)  Anonymous inner classes are handy when you want to define callbacks without writing a lot of code.

 

16.  An object that comes from an inner class has an implicit reference to the outer class object that instantiated it. Through this pointer, it gains access to the total state of the outer object. static inner classes do not have this added pointer.

 

17.  A method could refer to the data fields of the object invoking the method. An inner class method gets to access both its own data fields and those of the outer object creating it.

 

18.  The outer class reference is set in the constructor. The compiler modifies all inner class constructors, adding a parameter for the outer class reference.

 

19.  Only inner classes can be private. Regular classes always have either package or public visibility.

 

20.  The proper syntax for the outer reference is OuterClass.this.

 

21.  You can invoke the inner object constructor more explicitly using:

outerObject.new InnerClass(construction parameters)
  

 

22.  You refer to an inner class as OuterClass.InnerClass when it occurs outside the scope of the outer class.

 

23.  Inner classes are a phenomenon of the compiler, not the virtual machine. Inner classes are translated into regular class files with $ (dollar signs) delimiting outer and inner class names, and the virtual machine does not have any special knowledge about them.

 

24.  When you run javap -private OuterClass$InnerClass, you can see the compiler has generated an additional final instance field, this$0, for the reference to the outer class. You can also see the OuterClass parameter for the constructor.

 

25.  Inner classes are genuinely more powerful than regular classes because they have more access privileges. When you run javap -package OuterClass, you can see the compiler has generated a package access static method (with name like access$0 and accept outer class instance as parameter) to access the private field of outer class if inner class contains codes to visit it. And the compiler has replaced the access to the outer class field in inner class with that generated method.

 

26.  To summarize, if an inner class accesses a private data field, then it is possible to access that data field through other classes added to the package of the outer class, but to do so requires skill and determination. A programmer cannot accidentally obtain access but must intentionally build or modify a class file for that purpose.

 

27.  For private inner class, there are no private classes in the virtual machine, so the compiler produces the next best thing: a package-visible class with a private constructor:
private OuterClass$InnerClass(OuterClass)
And there is a second package-visible constructor
OuterClass$InnerClass (OuterClass, OuterClass$1)
And the compiler translates the call of outerObj.new InnerClass() to

new OuterClass$InnerClass(outerObj, null)

 

28.  You can define the class locally in a single method. Local classes are never declared with an access specifier. Their scope is always restricted to the block in which they are declared.

 

29.  Local inner classes can not only access the fields of their outer classes but also even access local variables. However, those local variables must be declared final.

 

30.    The local inner class must have copied the local variable of its declaring method as an instance field, before the local variable value went away when the method exits. The compiler synthesizes the name OuterClass$1InnerClass for the local inner class. The compiler detects access of local variables, makes matching instance fields for each one of them, and copies the local variables into the constructor so that the instance fields can be initialized. When you use javap to peek at the local inner class, you can see the compiler has generated an additional final field (with name like var$LocalVariableName) to store the local variable it accesses and added one more parameter to the constructor to pass in the local variable value.

 

31.  You don’t have to initialize a final variable when you define it. A final variable that isn’t initialized when it is defined is often called a blank final variable.

 

32.  If you do want to change the local variable in the local inner class, you can use an array of length 1. The array variable is still declared as final, but that merely means that you can’t have it refer to a different array. You are free to mutate the array elements.

 

33.  If you want to make only a single object of a local inner class, you don’t even need to give the class a name. Such a class is called an anonymous inner class. The syntax is:

new SuperType(construction parameters)
   {
     inner class methods and data
   }

Here, SuperType can be an interface (the inner class implements it ) or a class ( the inner class extends it ).

 

34.  The anonymous inner class doesn’t have a name thus it cannot have a constructor, you can initialize it via instance initializer block.

 

35.  If your equals methods use a test:

if (getClass() != other.getClass()) return false;

An anonymous subclass will fail this test.

 

36.  getClass() fails in a static method, Use the following expression instead:

new Object(){}.getClass().getEnclosingClass()

Here, new Object(){} makes an object of an anonymous subclass of Object, and getEnclosingClass gets its enclosing class—that is, the class containing the static method.

 

37.  If you want to use an inner class simply to hide one class inside another—but you don’t need the inner class to have a reference to the outer class object. You can suppress the generation of that reference by declaring the inner class static. A static inner class is exactly like any other inner class, except that an object of a static inner class does not have a reference to the outer class object that generated it.

 

38.  Inner classes that are declared inside an interface are automatically static and public.

 

39.  You can use a proxy to create, at runtime, new classes that implement a given set of interfaces. Proxies are only necessary when you don’t yet know at compile time which interfaces you need to implement. To construct an instance of an actual class, you can simply use the newInstance method or use reflection to find a constructor. But you can’t instantiate an interface. You need to define a new class in a running program. To overcome this problem, some programs generate code, place it into a file, invoke the compiler, and then load the resulting class file. Naturally, this is slow, and it also requires deployment of the compiler together with the program.

 

40.  The proxy class can create brand-new classes at runtime. Such a proxy class implements the interfaces that you specify. In particular, the proxy class has the following methods:
    a)    All methods required by the specified interfaces
    b)    All methods defined in the Object class

 

41.  An invocation handler is an object of any class that implements the InvocationHandler interface. That interface has a single method:

Object invoke(Object proxy, Method method, Object[] args);

Whenever a method is called on the proxy object, the invoke method of the invocation handler gets called, with the Method object and parameters of the original call. The invocation handler must then figure out how to handle the call.

 

42.  To create a proxy object, use the newProxyInstance method of the Proxy class. The method has three parameters:
    a)  A class loader. As part of the Java security model, different class loaders can be used for system classes, classes that are downloaded from the Internet, and so on. We can specify null to use the default class loader.
    b)  An array of Class objects, one for each interface to be implemented.
    c)  An invocation handler.

 

43.  Proxies can be used for many purposes, such as
    a)  Routing method calls to remote servers
    b)  Associating user interface events with actions in a running program
    c)  Tracing method calls for debugging purposes

 

44.  The proxy objects belong to a class that is defined at runtime. It has a name such as $Proxy0.

 

45.  All proxy classes extend the class Proxy. A proxy class has only one instance field—the invocation handler, which is defined in the Proxy superclass. Any additional data required to carry out the proxy objects’ tasks must be stored in the invocation handler. All proxy classes override the toString, equals, and hashCode methods of the Object class and these methods simply call invoke on the invocation handler.

 

46.  There is only one proxy class for a particular class loader and ordered set of interfaces. That is, if you call the newProxyInstance method twice with the same class loader and interface array, you get two objects of the same class. You can also obtain that class with the getProxyClass method of the Proxy class.

public static Class<?> getProxyClass(ClassLoader loader,                                      Class<?>... interfaces) throws IllegalArgumentException

 

47.  A proxy class is always public and final. If all interfaces that the proxy class implements are public, the proxy class does not belong to any particular package. Otherwise, all non-public interfaces must belong to the same package, and the proxy class will also belong to that package.

 

48.  You can test whether a particular Class object represents a proxy class by calling the isProxyClass method of the Proxy class.

分享到:
评论

相关推荐

    Core Java 9th Edition(Vol1,Vol2)

    Interfaces and Inner Classes Chapter 7. Graphics Programming Chapter 8. Event Handling Chapter 9. User Interface Components with Swing Chapter 10. Deploying Applications and Applets Chapter 11. ...

    Java Programming for Kids

    Interfaces, Lambdas, Abstract and Anonymous Classes Chapter 6. Access Levels, Collections, Generics, and Multi-Threading Chapter 7. Introduction to GUI with JavaFX Chapter 8. Programming User ...

    Java.7.A.Comprehensive.Tutorial

    Chapter 9 Interfaces and Abstract Classes Chapter 10 Enums Chapter 11 The Collections Framework Chapter 12 Generics Chapter 13 Input/Output Chapter 14 Nested and Inner Classes Chapter 15 Swing Basics ...

    Android Programming: The Big Nerd Ranch Guide, 3rd Edition

    Chapter 6. Android SDK Versions and Compatibility Chapter 7. UI Fragments and the Fragment Manager Chapter 8. Displaying Lists with RecyclerView Chapter 9. Creating User Interfaces with Layouts and ...

    Android.Programming.The.Big.Nerd.Ranch.Guide.2nd.Edition.0134171454.epub

    Android Programming: The Big Nerd Ranch Guide is an introductory Android book for programmers with Java experience. Based on Big Nerd Ranch's popular Android Bootcamp course, this guide will lead you ...

    Java.SE.7.Programming.Essentials

    分享的图书,有关Java SE 7 SDK标准的书,全英文。相应章节: Chapter 1. Introducing the Basics of ... Understanding Java Interfaces and Abstract Classes Chapter 11. Throwing and Catching Exceptions in Java

    Senfore_DragDrop_v4.1

    Drag and Drop Component Suite Version 4.1 Field test 5, released 16-dec-2001 ?1997-2001 Angus Johnson & Anders Melander http://www.melander.dk/delphi/dragdrop/ ---------------------------------------...

    《Programming in Java Second Edition》高清完整英文PDF版

    The second edition of Programming in Java confirms to Java Standard Edition 7, the latest release since Oracle took over Sun Microsystems. It is significant in the sense that the last update was six ...

    Java in easy steps: Covers Java 9, 6th Edition - epub格式

    Java in easy steps, 6th edition instructs you how to easily create your own exciting Java programs. Updated for Java 9, which was released September 2017, it contains separate chapters on the major ...

    Designing.Voice.User.Interfaces.epub

    Voice user interfaces (VUIs) are becoming all the...Chapter 6. User Testing for Voice User Interfaces Chapter 7. Your Voice User Interface Is Finished! Now What? Chapter 8. Voice-Enabled Devices and Cars

    莱迪思最新Lattice MachXO3 系列CPLD 官方技术文档资料合集.zip

    fpga-tn-02057-1-3-implementing-high-speed-interfaces-machxo3.pdf fpga-tn-02058-1-5-machxo3-sysclock-pll-design-user-guide.pdf fpga-tn-02060-1-2-memory-usage-guide-machxo3.pdf fpga-tn-02061-1-3-machxo3...

    Learning.Web.Development.with.Bootstrap.and.Angular.2nd.Edition.epub

    Get familiar with Bootstrap's new grid and helper classes Embrace TypeScript and EcmaScript 6 to write more maintainable code Use custom directives for Bootstrap 4 with the ng2-bootstrap library ...

    Get Programming with Go

    Chapter 6. Capstone: The Vigenère Cipher Unit 3 - BUILDING BLOCKS Chapter 1. Functions Chapter 2. Methods Chapter 3. First-Class Functions Chapter 4. Capstone: Temperature Tables Unit 4 - ...

    Micro CMOS Design, 2012.pdf

    5),.oversampling.converters.(Chapter.6),.high-resolution.converters.(Chapter.7),.and.syn- thesizers.and.clock.recovery.(Chapter.9)..All.feedback.systems.such.as.opamps,.∆Σ.modu- lators,.and.PLLs.are...

    Thinking in Java 4th Edition

    Java SE5 and SE6 .................. 2 Java SE6 ......................................... 2 The 4th edition........................ 2 Changes .......................................... 3 Note on the ...

    TMS320 DSP Algorithm Standard Rules and Guidelines

    3.1 Interfaces and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 3.1.1 External Identifiers . . . . . . . . . . . . . . . . . ...

    java8stream源码-Java8-Functional-Interfaces-and-Lambda-Expressions:Java8-

    java8流源码Java8-Functional-Interfaces-and-Lambda-Expressions 为什么使用 Lambda 启用函数式编程:我们可以将函数用作可以传递的实体。 可读且简洁的代码:强大的匿名内部类。 Lambda 使其可读且简洁。 更易于...

    Writing Testbenches using System Verilog

    Block and Core Verification . . . . . . . . 82 ASIC and FPGA Verification . . . . . . . 84 System-Level Verification . . . . . . . . . . 84 Board-Level Verification . . . . . . . . . . . 85 ...

    Fluent.Python.1491946008

    Object-oriented idioms: build classes by learning about references, mutability, interfaces, operator overloading, and multiple inheritance Control flow: leverage context managers, generators, ...

    java.核心技术.第八版 卷1

    For thorough coverage of Java fundamentals–including interfaces and inner classes, GUI programming with Swing, exception handling, generics, collections, and concurrency–look for the eighth edition ...

Global site tag (gtag.js) - Google Analytics