Java Basics - Inner Classes

Java inner class study notes

 

Overview:

Java class is single inheritance, java inner class is to achieve the ability to inherit multiple concrete or abstract classes - multiple inheritance.

Or the implementation of a certain class does not want or need to be set to public, such as directly instantiating the anonymous inner class A new parentClass(){...}, the implementation in this inner class is only used by the current outer class, Other outer classes have their own personalized inner class B that implements new parentClass(){...}. Although both anonymous inner classes inherit parentClass, inner class A and inner class B are only in their respective It will not be used by other classes, so there is no need to create this class separately, and it will only be implemented as an inner class.

 

Location classification:

1. Member inner class 2. Local inner class 3. Scope inner class (any scope if for) 4. Interface inner class

|

|

Each location can contain: normal inner classes, anonymous inner classes, static (nested) inner classes

 

Details:

1. Member inner class

    -- It is a member attribute of the class, controlled by access rights, default, private, protected, public

    -- Three ways to get members of inner classes:

        a. public InnerClass getInnerClass(){} is defined in an external class, call this method in another class to get

        b. OutClass outer = new OutClass(); OutClass.InnerClass inner = outter.new InnerClass(); In this way, if the permission of the inner class is controlled as private in the outer class, it cannot be used, and the compilation will fail.

        c. Define another member property in the outer class, the type is a member inner class and instantiate a new object, you can use it in another class according to the corresponding permissions

2. Local inner class

    -- The inner class is defined in the method, you can only new an inner class instance in the method body, assign it to a local variable or return directly

3. Scope inner class

   --Defined in if or other scopes, and new instances can only be used in that scope

4. Interface inner class

  --The inner class in the interface is automatically turned into public static, usually the inner class can implement some methods of the outer interface, and these methods implement the general implementation for other implementation classes

 

=======

5. Ordinary inner class

  --No static modification, no static member properties, nor other static inner classes (nested inner classes)

  --Implicitly contains a pointer to the outer class object, which depends on the outer class object. If there is no outer class object, the inner class object cannot be created

6. Anonymous inner classes

  -- new ClassA{ private String str1; public void method(){}} refers to the creation of an anonymous class object that inherits from ClassA. ClassA can be a specific class or an abstract class

  -- new InterfaceA{public void method(){}} means that an implementation interface InterfaceA is created and the method method is implemented

 -- Can be used for member properties or local method properties

7. Static inner class (nested)

 -- Modified by static, can be nested in static inner classes, can be static members

 -- No implicit pointers to external classes, no dependencies on external class objects

 

 

Reference from "Thinking In Java"

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852876&siteId=291194637