Inner classes in Java

Source: Internet
Author: User

Inner class?

There are classes in a class that implements the hiding of classes, implements multiple inheritance, and internal classes have access modifiers for properties and methods owned by all external classes
Avoid modifying interfaces with the same name method

The inner class is divided into four types:

member inner class, local inner class, Static inner class, Anonymous inner class

The first member of the inner class

 PackageCn.inner.member;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classAnimal { Public intAge ; PrivateString name;  Public voideat () {}protected voidShow () {}classDog { Public voidPlay () {System.out.println (name);        Eat (); }}} Test class PackageCn.inner.member;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classTest { Public Static voidMain (string[] args) {//create an instance of an external class;Animal.dog Dog =NewAnimal ().NewDog ();    Dog.play (); }}

member Inner class
1 if we want to access the inner class, we must have an instance of the outer class! Accessible only through external classes
2 member internal classes can have private, protected access rights

Second local inner class

 Packagecn.inner.local;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classAnimal { Public voidEat (String food) {//local inner class        classDog { Public voidShow () {System.out.println (food); }        }        //create an instance of an inner class        NewDog (). Show (); }} Test Class Packagecn.inner.local;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classTest { Public Static voidMain (string[] args) {Animal Animal=NewAnimal (); Animal.eat ("Liu Yangyang"); }}

Local inner class

1 classes defined in the method body or scope
2 internal use of method bodies only
3 when the formal parameters of our method need to be used in the inner class! Then this parameter must be final modified (not added (final) in jdk1.8)

The third type of static inner class

 PackageCn.inner.statics;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classAnimal {//non-static zodiac     Public intAge ; //Static Properties     Public StaticString name; //non-static methods     Public voideat () {}//Static Methods     Public Static voidrun () {System.out.println ("Static Inner Class"); }    //static inner class    Static classDog { Public voidShow () {run (); }}} Test class PackageCn.inner.statics;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classText { Public Static voidMain (string[] args) {Animal.dog dog1=NewAnimal.dog ();    Dog1.show (); }}

Static Inner class
1 does not depend on external classes
2 adding static to the inner class
3 cannot access external non-static methods and properties

Fourth Type Anonymous Inner class  

 Packagecn.inner.anonymous;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public Abstract classAnimal {Abstract voideat ();} Test class Packagecn.inner.anonymous;/*** @ April 6, 2018 * @ wanghong * @Administrator*/ Public classTest { Public Static voidMain (string[] args) {Animal Animal=NewAnimal () {@Overridevoideat () {System.out.println ("Anonymous inner class");        }        };    Animal.eat (); }}

Anonymous inner class
1 No Name, no definition of class
2 requires that all anonymous inner classes must implement an interface or inherit a class
3 when the formal parameters of our method need to be used in the inner class! Then this parameter must be final modified (not added (final) in jdk1.8)

Inner classes in Java

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.