Thinking in Java Note 10 Internal class

Source: Internet
Author: User
* ** Access

1. when you create an internal Class Object outside of an external class (non-static method), you must use outerclass. innerclass to specify type 2. the internal class has access to all elements of the external class, and the internal class implicitly stores a reference pointing to the external class object. 3. In the internal class of this. New, you can use outerclass. This To obtain the reference of the external class object. Internal class objects cannot be directly produced, but are created through external class objects, such

      OutClass outer= new OutClass();
OuterClass .InnerClass inner=outer. new InnerClass();
4. The external class can access the private element of the internal class.
* ** Private internal classes can completely block any type-dependent encoding and completely hide the implementation details. It cannot be accessed except outerclass.
* ** Internal classes can be used in any place within the scope of use. Like variables, internal classes have a zone and are not valid outside the zone.
* ** When an anonymous internal class uses an externally defined object in an anonymous internal class, the parameter reference must be final.

     public class Parcel9 {
public Destination destination( final String dest) {
return new Destination() {
private String label = dest;
public String readLabel() { return label ; }
};
}
public static void main(String[] args) {
Parcel9 p = new Parcel9();
Destination d = p.destination( "Tasmania" );
}
}

There is no name constructor in the anonymous class, but through instance initialization, you can simulate the constructor effect:

     abstract class Base {
public Base( int i) {
print( "Base constructor, i = " + i);
}
public abstract void f();
}

public class AnonymousConstructor {
public static Base getBase( int i) {
return new Base(i) {
{ print( "Inside instance initializer" ); }
public void f() {
print( "In anonymous f()" );
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
}


* ** The internal class of the nested class is static 1. to create an object of A nested class, you do not need an object of its peripheral class. 2. you cannot access non-static peripheral class objects from nested class objects. 3. Common internal classes cannot contain static data and fields, nor Nested classes. Nested classes can contain.
* ** Why use an internal class to inherit from a class or implement an interface, and use code operations of the internal class to create its peripheral class objects, therefore, we can think that the internal class provides a window for entering its external class. To implement multi-inheritance of classes, you must use internal classes as follows:

     class D {}
abstract class E {}

class Z extends D {
E makeE() { return new E() {}; }
}

public class MultiImplementation {
static void takesD(D d) {}
static void takesE(E e) {}
public static void main(String[] args) {
Z z = new Z();
takesD(z);
takesE(z.makeE());
}
}

Internal class features: 1 internal class can have multiple instances, each instance has its own status information, and its peripheral Class Object Information is independent of each other. 2. In a single peripheral class, multiple internal classes can implement the same interface in different ways or inherit the same class. 3. Creating an internal class object does not depend on creating an external Class Object. 4. The internal class has no confusing is-a relationship. It is an independent entity.
Callback can be implemented through internal classes and interfaces in internal classes to provide a hook for external calls. The value of callback lies in its flexibility. It can dynamically decide the method to be called during runtime.
* ** Internal class inheritance because the internal class constructor must be connected to a reference pointing to its external class object, the following method is required to inherit the internal class:

     class WithInner {
class Inner {}
}

public class InheritInner extends WithInner.Inner {
InheritInner(WithInner wi) {
wi. super ();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}

* ** You can create an internal class in a code block for a local internal class. It cannot have access specifiers and is not part of the peripheral class. It can access all the members of the peripheral class and the variables in the current method and is only valid within the method range. Use it instead of the anonymous internal class: 1. A named constructor is required, or the constructor needs to be overloaded. The anonymous internal class can only be used for instance initialization. 2. More than one internal class object is required.

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.