Inheritance of classes within Java (with source code)

Source: Internet
Author: User

Objective
Because the constructor of the inner class must be connected to a reference to its perimeter class object, things can get a little complicated when inheriting the inner class. The problem is that the "secret" reference to the perimeter class object must be initialized, and there is no longer a concatenated default object in the exported class. To solve this problem, you must use special syntax to clearly clarify the relationship between them:
Sample source Code
Package Com.mufeng.thetenthchapter;class Withinner {public Withinner () {//TODO auto-generated constructor StubSystem.out.println ("Withinner constructor");} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "Withinner";} Class Inner {public Inner () {//TODO auto-generated constructor StubSystem.out.println ("Inner constructor");} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "Inner";}}} public class Inheritinner extends Withinner.inner {public Inheritinner (Withinner WI) {//TODO auto-generated constructor s Tubwi.super (); System.out.println ("Inheritinner constructor");} @Overridepublic String toString () {//TODO auto-generated method Stubreturn "Inheritinner";} public static void Main (string[] args) {Withinner wi = new Withinner (); Inheritinner II = new Inheritinner (WI); SYSTEM.OUT.PRINTLN (ii);}}

Result output
Withinner Constructorinner Constructorinheritinner Constructorinheritinner

SOURCE parsing
can see thatInheritinnerInherit from the inner class only, not the perimeter class. However, when you want to build a constructor, the default constructor is not good, and you cannot just pass a reference to a perimeter class object. In addition, the following syntax must be used inside the constructor:
Enclosingclassreference.super ()
This provides the necessary references before the program can be compiled.



Inheritance of classes within Java (with source code)

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.