Java Programming Idea (second Edition) No. 08 Chapter: Interface and Implicit class

Source: Internet
Author: User
Tags abstract anonymous inheritance interface modifiers reference multiple inheritance in c
Programming
My notes.



Interfaces&inner classes provides a more sophisticated way of organizing and controlling the objects in your system.

Interface is better than abstract class, because we can write a class that "can be upcasting to multiple base classes" to achieve multiple inheritance in C + +.

You can add the keyword public before the keyword interface (but only if the interface is defined as a file with the same name), otherwise it becomes "friendly", Can only be used within the same package. All methods within interface will be public by default. Because interface does not have any implementation details, which means that interface is not associated with any storage space, Merging multiple interface is therefore an easy task, and compilers do not need to be formidable enemy. Therefore, when merging multiple interfaces, the problems faced by C + + will not appear in Java. Obviously we should try to consider using interface rather than abstract class.

Generates Chang (grouping constants) because all data members in interface automatically become static and final, so for the generation of Chang (like an enum in C or C + +), Interface is a very handy tool. The data members in the interface automatically become public, so there is no need to understand the mark.

Name conflicts when interface merges (name collision) Problem: When you implement multiple interfaces, you may fall into a small trap. overriding, implementation, Overloading all the unpleasant mix. And overload method cannot be distinguished by return type alone. So you should be very careful at this time.

Nested (nesting) interfaces: In addition to the realization of private interface This dongdong, really still useful? The book said: "Perhaps the original reason for joining these features is just for grammatical consistency, but I always think that when you know one of the features of a language, you can often find the usefulness of that feature." "

Inner Classes (implicit Class) from the design: The main purpose of Inner class should not be hidden, because Java has a very good hiding mechanism-friendly class. But when you start upcasting to base class and especially transition to interface, you can highlight the benefits of inner class: (Tip: From a "interface I" inner The class object produces a reference point I, which is essentially the same as "transition up to base class", because the inner class (the one who implements the I) can be completely unseen in the next context and is not used by anyone. In this way, we can easily "hide the implementation details." All you get is a reference that points to base class or interface. Note that here inner classes can have properties that the general (Non-inner) class cannot have: For example, you can have private and protected access modifiers. When declared private, the representation is for outer class access only, and when declared as protected, the representation can be used by any class within the package or by the successor of the outer class.

In methods and scopes within the inner classes afraid ~, mentioned above is the most normal use of inner class, if you need, he also provides some obscure use: you can inner Classes within the function or even as to any program category (scopes) ... You really need that? Faint.

Anonymous (anonymous) inner classes time again,i asked you: Do you really need it? Write more code will die!

Connection to peripheral (outer) class inner class also has the obvious benefit of having access to all the members of the perimeter object without adding any modifiers. In addition inner classes also has access to all elements of enclosing class (this is very different from C + +). This is because any inner class is generated

, you must record a reference, point to a particular object enclosing class (for example, outer Class), and if the compiler cannot find this ref, wait for the error.

Static inner classes (static implicit Class) and the above inner class on the contrary, if you do not need this ref, then it is static! This means: 1. When the object is generated, there is no need for a enclosing Class object. 2. You cannot access enclosing class object members in the static inner class object in fact, from another point of view, there is a 3rd difference: 3. You can have a static data,static fields, Static inner class. However, non-static inner class is not available. Thus we can find that the static inner class is similar to the static method. 4. You can put the static inner class in the inte Rface inside--static Inner class is just placed in the interface namespace, using this property, you can put main () in an interface to support testing ...

Object with (referring) Outer class [outer class name].this.xxx if you want to generate a outer class object directly inside the static function in the inner class, you need to take care of a outer Class of ref p, and then p.new [inner class name]. Because we said that inner class must have a outer class ref when it is generated, otherwise it will be considered an error. Of course, for the static inner class You don't have to.

Extend the antennae from a multi-tiered nested class regardless of how deep the inner class is nested. All of its outer classes members are accessible to it (consider namespace).

Inherit inner classes: first vomiting blood 3 liters, really necessary? The difficulty is that the mysterious ref must be initialized. But there is no default object that can be connected within the derived class. Therefore, special syntax support is required: Enclosingclassreference.super ();

Can inner classes be made up? Finally, the author has explained, remember the result can be: "In fact, no use."

Inner class identifier (identifiers): $

Summary: Why need inner classes?1. Implemented by inner class interface can provide outer class to achieve interface features: you can explore the implementation of the details. Inner class is not limited by outer Class has been inherited from an implementation class. In other words, inner classes internship because you inherited multiple non-interface.2.closures (end) and callbacks (callback) 3.Control frameworks



My question.


What are the advantages of putting a static inner class containing main () in the interface to test the inheritance main () in the previous paragraph?


What are the advantages of putting a static inner class containing main () in the interface to test the inheritance main () in the previous paragraph?



What are the advantages of putting a static inner class containing main () in the interface to test the inheritance main () in the previous paragraph?

Originally protected>=friendly, I am dirt.

Is the practical value really great for some inner class content? Is it worthwhile to spend 80% of your study time for 20% of the possibilities? I think a lot of things are prepared for dead-headed programmers, and I'm obviously not going to do it and I don't have the ability.

There are too many problems ... Come slowly. Thank you for your advice.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.