Java class inheritance

Source: Internet
Author: User
Tags export class

Java class inheritance

The constructor of the Java internal class must be connected to the reference pointing to its peripheral Class Object (the constructor of the internal class must give it an external Class Object reference, and the internal class depends on the external class object ), therefore, when inheriting internal classes, you must manually add a call to the base class constructor In the constructor of the export class.

Because there is no peripheral class object during export class instantiation, so that the instance of the export class can connect to it.

Therefore, we need to create a peripheral class, and then use a specific syntax to indicate the relationship between the internal class and the peripheral class.

In the following example, you need to give the export class InheritInner a reference from the internal class's peripheral class. For normal inheritance, you only need to add super () to the export class constructor, and the internal class requires the reference of. super () in the peripheral class object ();

1 class WithInner {2 class Inner {} 3} 4 public class InheritInner extends WithInner. inner {5 InheritInner (WithInner wi) {6 wi. super (); // The wi parent class is object 7} 8 public static void main (String [] args) {9 WithInner wi = new WithInner (); 10 InheritInner ii = new InheritInner (wi); 11} 12}

What should I do when the inherited internal class only has 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 a parameter, it should be passed to this super function} public static void main (String [] args) {WithInner wi = new WithInner (); InheritInner ii = new InheritInner (wi );}}

 

Related Article

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.