Inherit from inner class

Source: Internet
Author: User

Because the inner class builder must be associated with a handle to the encapsulated class object, the situation becomes slightly more complex when inheriting from an inner class. The problem here is that the "secret" handle of the encapsulated class must be initialized, and there is no longer a default object in the derived class that can be connected. The solution to this problem is to use a special syntax that explicitly establishes this association:

: Inheritinner.java
//Inheriting an inner class

class Withinner {
  class Inner {}
}

Heritinner 
    extends Withinner.inner {
  //! Inheritinner () {}//Won ' t compile
  inheritinner (Withinner wi) {
    wi.super ();
  }
  public static void Main (string[] args) {
    Withinner wi = new Withinner ();
    Inheritinner II = new Inheritinner (WI);
  }
///:~

As you can see, Inheritinner only extends the inner class and does not extend the external classes. But when you need to create a builder, the default object is meaningless, and we can't just pass a handle to the encapsulated object. In addition, the following syntax must be used in the builder:
Enclosingclasshandle.super ();
It provides the necessary handles so that the program compiles correctly.

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.