javase--abstract class, inner class, interface

Source: Internet
Author: User
Tags naming convention

first, abstract class

When things cannot be described in detail, they can abstract things, simply describe the behavior they should have and not describe them in depth, thus creating an abstract class, and using the abstract keyword to modify the inner method of the class is also described with abstract.

1. Features

  An abstract method can only be defined in an abstract class and cannot have a method body, an abstract class cannot be instantiated, an object cannot be created with the New keyword, its subclasses can be instantiated after overriding all abstract methods, and if the subclass inherits the abstract class but does not overwrite the abstract, the subclass is also an abstract class . A non-abstract method can exist in an abstract class, that is, a method that has been implemented that can be overridden or used by subclasses.

2. Pitfalls

  A constructor exists in an abstract class and is instantiated only for the object of the subclass, and the abstract class must be the parent class that requires the subclass to inherit the abstract class and override the abstract method, and the abstract class can not define an abstract method, simply to make the class impossible to instantiate; abstract and final, private, Static cannot coexist , abstract classes may not contain abstract methods, classes containing abstract methods must be abstract classes, and attribute definitions in abstract classes are the same as for generic classes.

two . Interface (interface)

Interface is a collection of method definitions and constant values, and interfaces are a special kind of abstract class, all of which are abstract methods. He does not have a constructor for an interface, so his variables can only be defined as static constants, so do not initialize the variables with constructors.

1. Features
    • Interfaces cannot be instantiated, subclasses need to overwrite all abstract methods in order to instantiate, otherwise the subclass is an abstract class, the interface is used to be implemented.
    • The reason a class cannot inherit more is that when the same method declaration and implementation occurs in two parent classes, the subclass calls the method with uncertainty, which is caused by the method body, and the interface can inherit more because the method in the interface is not implemented without the method body. When the two parent interface is implemented, the subclass overrides the parent interface with the same name method, which defaults to the definition of a behavior method so that only one method with the same name will be implemented. The essence of inheritance is to acquire the basic function of the system, and to extend the function is realized by implementing different interfaces.
    • An interface has an inheritance relationship with an interface that does not have an implementation relationship, and the interface can be multiple-inheritance (because only method declarations do not have a method body so there is no conflict), and interfaces are not constructed (because their member properties are constants).
    • Interfaces are exposed rules that require the user to implement the specification.
2. Interface and class differences
    • There are two common members of the interface: Global constants, abstract methods, and all have fixed modifier commonalities: Members are decorated with public, and the default check is to provide public automatically at compile time;
    • Member Definition method: public static final int MAX = 5;
    • Method definition: Public abstract void fun ();
three . Inner class

Defining one or more classes that belong to the class inside a class is called an inner class, an inner class is equivalent to a member of an external class, and you can freely access members of an external class, but an external class needs to create an instance of the inner class to access the inner class, and in the main method the inner class is the outer class name + "." The inner class name is the reference type of the inner class (Text.inner ti = new Text (). New Inner ();) the important role of inner classes is to provide support for multiple inheritance.

When a class has an inner class that is compiled to produce two class files that contain the inner class, the class file naming convention is: the outer class name @ Inner class name, the external class name @1 inner class is called the local inner class, because the class is in the local method, Because the inner class is separated from the outer class in principle, the inner class is not loaded when the external class object is created;

Internal classes do not allow access to local variables that are local because the declaration period is different, the local inner class can only access the final decorated local variables, and the inner class needs to access the member variable with the same name as the outer class, and this is used to determine the specific object (Inner.this;outer.this).

1. Anonymous inner class

An anonymous inner class is a simplified inner class in which an inner class needs to inherit or implement an external class or interface, in the form of a new parent class name or interface name () {subclass content}. A subclass member; the place where anonymous inner classes often appear is the method body or property initialization.

2. Non-static inner class

A non-static inner class does not have a parameterless constructor, and its default constructor requires an external class instance parameter, which also conforms to the rule of a non-static inner class: a non-static inner class must be parasitic in an instance of an external class, and no non-static inner class object exists without an external class object. When using new Inner () is displayed inside the outside class, the virtual machine's underlying will also pass the current this as an argument to the constructor when creating the inner classes! The declaration of the parameterless constructor in the inner class is also generated at compile time by the corresponding parameter constructor and implicitly called, which shows that defining a parametric constructor with a string parameter will also automatically carry an external class parameter when compiling! This shows that the non-static internal classes of the dependence of the external class how high!

Non-static inner classes are in a non-static context, so non-static inner classes are not allowed to have static members, and only static member constants are allowed to be defined.

3. Static Inner class

Static inner class, defined in a class, outside any method, defined with static, static inner class can only access static members of external classes. generating a static inner class does not require an external class member: This is the difference between a static inner class and a member inner class. objects of a static inner class can be generated directly: Outer.Inner in=new Outer.Inner (), rather than generated by generating an external class object. This actually makes the static inner Class A top class, and of course it can also be defined as a private static inner class.

4. The difference between static inner class and non-static inner class
    • Ordinary inner classes can access the various types of members of their external classes, but static internal classes only have access to static members
    • Ordinary inner classes cannot define various static members (including static variables, static methods, static code blocks, and static inner classes), while static inner classes can;

This syntax error occurs with ① static variables and static methods (static methods can only is declared in a static or top level type) means that the static method can only be static or top type (the top-level type should be the outer class Of course, static variables and static inner classes are the same. The reason is that both static and static methods need to be accessed through the class name, without any instantiation of the object, whereas the initialization of the ordinary inner class takes advantage of the instantiation of the outer class, which clearly violates the design intent of the static.

This syntax error ② static code block (cannot define static initializer in inner type Outer.Inner) means that a static initializer cannot be defined in the inner class. The reason is similar to the above, the members of the static declaration can only be common to the class, but not only one instanced object, the popular point is that no matter how many layers of reference, can only be a class to reference and cannot be an object.

javase--abstract class, inner class, interface

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.