Inheritance of Java inner classes

Source: Internet
Author: User
Tags export class

The constructor of the Java inner class must be connected to a reference to its perimeter class object (the constructed inner class must give it a reference to an outer class object, the inner class depends on the Outer class object), so when inheriting the inner class, you need to manually include the call to the base class constructor in the constructor of the exported class.

Because, when you export a class instantiation, there is no perimeter class object to allow an instance of the exported class to connect to it.

So, we need to create a perimeter class and then use a specific syntax to indicate the relationship between the inner class and the outer class.

In the following example, the export class needs to be inheritinner a reference from a perimeter class in the inner class. Ordinary inheritance, you simply add super () to the export class constructor, and the inner class requires a perimeter class object reference. Super ();

1 Class withinner{2     class inner{} 3} 4 public class Inheritinner extends withinner.inner{5     inheritinner (Withinn  Er wi) {6         wi.super ();//wi's parent class is Object 7     } 8 public     static void Main (string[] args) {9         Withinner WI = new Withinner ();         Inheritinner II = new Inheritinner (WI);     }12}

and further, what should be done when the inherited inner class has only a non-default constructor?

Class withinner{    class inner{public        Inner (int i) {            System.out.println (i);}}    } public class Inheritinner extends withinner.inner{    inheritinner (Withinner wi) {        int i=0;        Wi.super (i);//As shown in the code, arguments should be passed to this super function when the inherited constructor requires arguments {public    static void Main (string[] args) {        Withinner WI = new Withinner ();        Inheritinner II = new Inheritinner (WI);}    }        

Inheritance of Java inner classes

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.