Java Internal classes

Source: Internet
Author: User

Inner class

As the name implies, an inner class is a class defined internally

The inner class places the class inside other classes. Looks like a code-behind mechanism.

The inner class understands the perimeter class and can communicate with it.

The code written in the inner class is more elegant and clear.

Create an inner class

Write the definition of the class directly inside the class

Access characteristics of internal classes

Inner classes can access members of external classes directly, including private

External class to access members of an inner class, you must create an object

The external class name. Internal class Name Object name = External Class object. Inner Class object

Linking to an external object

When an object of an inner class is generated, there is a connection between this object and the peripheral object that made it.

So it can access all the members of its perimeter object without requiring any special conditions.

In addition, the inner class has access to all elements of its perimeter class.

When an object of a perimeter class creates an inner class object, the inner class object must secretly capture a reference to that outer class object.

Then, when accessing the perimeter class member, that reference is used to select the members of the perimeter class.

An object of an inner class can be created only if it is associated with an object of its enclosing class.

When building an inner class object, you need a reference to the perimeter class object.

. This and. New

If you need to generate a reference to an external class object, you can use the outer name immediately followed by the dot and this.

If you want to tell some other object, go to create an object of one of its inner classes. You can use other external class pairs to reference the root dot and the new

It is not possible to create an inner class object before owning an external class object.

Because the inner class object is secretly connected to its outer class object.

However, if you are all nested classes (static inner classes), you do not need a reference to the external class object.

Inner class and upward transformation

Inner classes are useful when you are transforming an inner class up to its base class, especially when transitioning to an interface.

From an object that implements an interface, a reference to this interface is made, and the effect is essentially the same as the base class of the object being transformed upward.

This is because this inner class---the implementation of an interface---can be completely invisible, and is not available.

All you get is a reference to a base class or interface, so it's easy to hide the implementation details.

When a reference to a base class or interface is obtained, it may not even be possible to find the exact type.

The private inner class can completely block any type-dependent encoding and completely hides the implementation details.

From the client programmer's point of view, due to the inability to access any newly added methods that are not originally public interfaces,

So the expansion interface is worthless. This also gives the Java compiler the opportunity to generate more funny code.

Inner classes within methods and scopes

If you implement a type of interface, you can create and return a reference to it

To solve a complex problem, you want to create a class to help solve it, but you don't want the class to be common.

A class that is defined in a method

A class defined in scope within the method.

An anonymous class that implements an interface

An anonymous class that extends classes with non-default constructors

An anonymous class that performs field initialization

An anonymous class that is constructed by instance initialization (anonymous classes cannot have constructors)

Anonymous inner class

Anonymous inner classes are one of the local inner classes and are a simplified notation.

The premise is that there is a class or interface. The class here can be either a concrete class or an abstract class.

The essence is an anonymous object that inherits the class or implements the subclass of the interface

Nested classes

If you do not need to have a connection between an inner class object and its perimeter class object, you can declare the inner class as static.

Such an inner class is called a nested class.

When the inner class is static:

To create objects of nested classes, objects of the perimeter class are not required

Non-static perimeter class objects cannot be accessed from nested class objects

The fields and methods of ordinary inner classes can only be placed on the outer level of the class, so normal inner classes cannot have static data and static fields, nor can they contain nested classes.

But nested classes can contain the above things.

Nested classes do not preserve this reference for external classes, similar to a static method

Nested classes can be part of an interface.

Inheritance of inner classes

Can the inner class be overwritten?

Local inner class

Inner class identifier

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.