Tij Reading Notes (eighth chapter)

Source: Internet
Author: User
Tags abstract anonymous define closure constructor final inheritance interface
Notes
8: Interface (interface) with internal class (inner class)

Interface (interface) can be imagined as a "pure" abstract class. Allows the developer to define the form of the class: The method name, the argument list, the type of the return value, but no body of the method. Interface can also contain data members, but it is inherently static and final.  Interface only provides form, not talk about realization.  Interface will be used as a "Protocol (protocol)" between classes. When you implements a interface, you have to define this interface method as public.    If you do not, then it will be programmed to package permissions, so that the access rights of these methods will be restricted, which is not allowed by the Java compiler.  Java's "Multiple Inheritance (multiple inheritance)". As long as the design of the base class does not include the definition of method-and-member variables, you should give priority to using interface.    Only when you have to define a method or a member variable can you change it to an abstract class, or to an entity class as needed. Name conflicts when merging interfaces with inherited extensions interface you can use inheritance to add new methods to interface, or you can combine multiple interface into a new interface with inheritance.    In both cases, all you get is a new interface.    The grouping of constants because interface data members automatically are static and final, interface is a handy tool for creating a set of constant values.    The members of the interface are automatically public. Nesting of data member interfaces in the initialization interface

Internal class     inner class and upload     in fact, the interface that passes the object to it is exactly the same as passing it on to the base class.     Ordinary classes (non-internal classes) can not be defined as private or protected; they can only be public or package permissions.     Internal classes in methods and scopes     Anonymous inner class     if you're defining an anonymous inner class, you'll need to use an outside object, That compilation will require you to put the reference life of this parameter into final. If you forget, you will get an error when compiling.     actually the power initialization process is the constructor of the anonymous inner class. Of course, its functionality is limited: You cannot overload instance initialization, so you can only have one constructor.     relationship with host class     if you create an inner class, the object of this inner class has a relationship with the object of the host class that created it (enclosing object). This allows it to access the members of the host class object-no special authorization is required. In addition, the inner class can access all the elements of the host class.     Nested Classes     If you don't need this "relationship between the inner class object and the Host class object, you can define the inner class as static." This is often referred to as "nested classes (nested Class)". The nested class means:      1. You can create objects of nested classes without the object of the host class.       2. Non-static host class objects cannot be accessed within the object of a nested class. There is no code inside the     interface, but nested classes can be part of the interface.     objects that reference the host class     unless you have also created objects for the host class, it is not possible to create objects of the inner class at all. However, if you create nested classes (static inner classes), then you do not need the reference of the host class object.     Outward access in multiple-layer absconded classes     when you create objects of multiple nested inner classes in another classWhat kind of grammar should be used. The ". New" statement indicates the correct scope, so you do not have to qualify the class name in the statement that calls the constructor.     Inheriting internal class     you must pass it to him. The reference of the host class object. In addition, you must also use this syntax in the constructor:      Enclosingclassreference.super ();    Inner class can be overwritten?     when you inherit the host class, the inner class trick is over. These two inner classes are two entities in each other's belly, all of which have their own namespaces.     Native internal class (local inner classes)     The only valid reason for using local internal classes to replace anonymous inner classes is that you need a constructor with a name and/or overload the constructor. Because anonymous inner classes can only perform strength initialization.     The only reason to select a local inner class instead of an anonymous inner class is that you have to create multiple objects of that kind.     Internal class identifier (Inner class Identifiers)

Why should there be internal classes? Each inner class can inherit an implementation (implementation) independently.  Therefore, the inner class is not bound by the "host class has inherited another implementation." With the inner class, you get the following additional features: 1.    An inner class can have multiple instances, and each can have its own state information that is independent of the host class object. 2.    There are several internal classes that can be placed in a host class, and they can implement the same interface or inherit the same class in different ways. 3.    The timing of the internal class object creation has nothing to do with the creation of the host class object. 4.    Inner class There is no "yes" relationship that makes people dizzy; He is an independent entity. The closure and callback (Closures & callbacks) Closure is an object that can be invoked to record information about the scope that created it.    An inner class is an object-oriented closure, because it not only holds all the information about the host class ("create its scope"), but it also automatically saves the reference to the object of that host class, not to mention that it has the right to manipulate all members of the object, even if they are private. The internal class and control framework (Inner classes & Controls Frameworks) "Application framework (Application framework)" is a class or set of classes designed to solve a particular type of problem. The control framework is one of the application frameworks, primarily for response time, and if the primary character of the system responds to events, it is called the event-driven system (Event-driven systems).

Summary: The concepts of interfaces and inner classes are more complex than most other OOP languages, but Java interfaces and internal classes are much simpler compared to C + + mi. Although these features are relatively simple in themselves, their specific usage is in the category of design, which is very similar to polymorphism.


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.