Inheritance (full) of Java inner classes _java

Source: Internet
Author: User
Tags export class inheritance

Below through the example code to share with you about the Java internal classes of inheritance, specific details are as follows:

The constructor of the Java inner class must be connected to a reference to its Outer class object (the constructed inner class must give it a reference to an external class object). An inner class relies on an external class object, so it is necessary to manually add a call to the base class constructor in the constructor of the exported class when inheriting the inner class.

Because, when you export a class instantiation, there is no outer-class object that allows an instance of the exported class to be connected to it.

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

In the following example, you need to inheritinner a reference from the outer class of the inner class to the derived class. Ordinary inheritance, you simply add super () to the export class constructor, and the inner class requires a peripheral class object reference. Super ();

Class withinner{
  class inner{}
 } public
 class Inheritinner extends withinner.inner{
  Inheritinner ( Withinner WI) {
     wi.super ();//wi parent class is Object
   } public
   static void Main (string[] args) {
    Withinner WI = new Withinner ();
    Inheritinner II = new Inheritinner (WI);
  }
 

And further, what happens when an 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, when the inherited constructor requires parameters, pass the parameter to this super function} public
  static void Main (string[] args) {
    Withinner WI = new Withinner ();
    Inheritinner II = new Inheritinner (WI);
  }
    

The above is the Java internal class inheritance detailed, hope to be able to help everybody.

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.