Java Internal classes

Source: Internet
Author: User

Inheritance

Multiple inheritance: Literally understand that subclasses can inherit multiple parent classes;

Inheritance classification: Public inheritance, protection inheritance, private inheritance.

Public Inheritance ( Public )

The public and protected members of the base class can be public members and protected members of their derived classes

member functions of derived classes can access public and protected members in the base class and cannot access private members in the base class

The object of the derived class can access the public members of the base class

Private Inheritance ( Private )

Both public and protected members of the base class are private members of their derived classes

In private inheritance, a member of a base class can only be accessed by a direct derived class and cannot be inherited further down

Protection C + + Class Inheritance ( protected )

All public and protected members of the base class become protected members of derived classes

the public and protected members of a base class can only be accessed by its direct derived class member function or friend .

——————————————————————————————————————————————————————————————————————————— from Baidu Encyclopedia


About inheritance: Inheritance is meant to share data as well as to encapsulate data. Sharing can be reused, with these features also the meaning of inheritance. And many inheritance is also a kind of inheritance, relatively speaking, sharing multiple objects of data, meaning is consistent, and logic will be cumbersome point. such as: In multiple inheritance, the different base classes in the derived class member functions or object access caused by the two semantic problem.

that's why Java Many inheritance is discarded, but the inner class solves the problem to some extent.


Inner class

An example of life --- crows drink Water. Of course this is a replica, assuming the crow can become a small fish. This eliminates the need for the use of external stones, slowly to drink water, but directly to drink water in the bottle. Here: Outer class ----- bottle; inner class ------- small fish. Non-static inner classes can access member variables of external classes.

Specific Use

General use


Classouter{privatestring id= "Outerid";          Public Outer () {                 innerin=new Inner ();                 In.print ();  } Protectedclass Inner  {publicvoid print () {          System.out.println (ID);}}}                 Publicclass testinnerclass{publicstatic void Main (string[] args) {    Outer oi=newouter ();}         }


if the access limit for an external class is Private , the inner class is also able to access the member variables of the external class. When an inner class is declared as a static inner class, it is inaccessible to member variables and functions of the outer class. But once declared as a static inner class, you can directly consider the inner class as a static member variable of the outer class. See next demo


static inner class


Classouter{privatestring id= "Outerid";          Public Outer () {                 innerin=new Inner ();                 In.print ();  } Protectedstatic class Inner  {publicvoid print () {          System.out.println ("Inner.id");}}}                 Publicclass teststaticinnerclass{publicstatic void Main (string[] args) {    Outer oi=newouter ();            Non-static classes are accessed directly as member variables, unlike the member variable here is a class              Outer.Inner in=new Outer.Inner ();}   }


The way in which static inner classes are accessed is somewhat like namespaces to define the class name.


use of anonymous internal classes

Interfaceperson{publicstring getName ();p ublicstring getage ();p ublicstring getheight (); Classstudent{publicvoid testnoname (person p) {System.out.print (P.getname () +p.getage () +p.getheight ());}} Publicclass nonameinnerclass{public    static void Main (string[] args)    {    Student s=new Student ();    S.testnoname    (      ///Anonymous inner class passed in to resolve no need to implement this interface to invoke the associated method         new person ()         {public         stringgetname () {return " NAME:CFL ";} Publicstring Getage () {return "age:24";}                                        Publicstring getheight () {return "height:1.7";}}         );}}


In summary: The greatest advantage of the inner class is the ability to access the resources of the external class, and of course only non-static inner classes. This anonymous inner class is also used when the interface needs to be implemented only once. This is still relatively practical.

Java Internal 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.