Java inner class "Overwrite"

Source: Internet
Author: User

Think in Java P269

If the inner class in the subclass is the same as the inner class in the parent class, it seems that the subclass inner class "Overrides" The inner class of the parent class, but does not actually represent anything.

 Public classBigeggextendsEgg { Public classYolk { Publicyolk () {System.out.println ("Bigegg.yolk ()"); }    }     Public Static voidMain (string[] args) {NewBigegg (); }}classegg{Privateyolk y; protected classYolk { Publicyolk () {System.out.println ("Egg.yolk ()"); }    }     PublicEgg () {System.out.println ("New Egg ()"); Y=Newyolk (); }}

Output Result:

New Egg ()
Egg.yolk ()

The result shows that the inner class does not exhibit other characteristics in inheritance, and the two are two completely independent entities.

classEGG2 {protected classYolk { PublicYolk () {print ("Egg2.yolk ()"); }         Public voidF () {print ("Egg2.yolk.f ()"));} }    PrivateYolk y =Newyolk ();  PublicEgg2 () {System.out.println ("New Egg2"); }     Public voidInsertyolk (yolk yy) {y =yy;}  Public voidg () {y.f ();}  Public voidprint (String msg) {System.out.println (msg); }} Public classBigEgg2extendsEGG2 { Public classYolkextendsEgg2.yolk { PublicYolk () {print ("Bigegg2.yolk ()"); }         Public voidF () {print ("Bigegg2.yolk.f ()")); }    }     PublicBigEgg2 () {Insertyolk (Newyolk ()); }     Public Static voidMain (string[] args) {Egg2 e2=NewBigEgg2 ();    E2.G (); }     Public voidprint (String msg) {System.out.println (msg); }}

Output:

Egg2.yolk ()
New EGG2
Egg2.yolk ()
Bigegg2.yolk ()
Bigegg2.yolk.f ()

1. Parent class Variable Initialization

Private yolk y = new yolk ();
The yolk of the parent class is called, so the result of the output is Egg2.yolk ()

2. Parent class constructor, output new EGG2

3. Sub-class constructors

Insertyolk (new yolk ());
Using the yolk of the subclass, instantiating the subclass, you need to instantiate the parent class, so the result of the output is
Egg2.yolk ()
Bigegg2.yolk ()

4. Code E2.G ();

The yolk in the parent class is actually the yolk of the subclass, so the result of the output is Bigegg2.yolk.f ()

Java inner class "Overwrite"

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.