Abstract methods and abstract classes
Abstract methods and abstract classes must be modified using abstract. classes with abstract methods can only be defined as abstract classes. abstract classes can have no abstract methods.
Rules they follow:
1. Use abstract modification, and the abstract method does not have a method body (note the difference with the empty method body ).
2 abstract classes cannot be instantiated, even if the abstract class does not contain abstract methods
3. the abstract class contains six types: attribute, method, initialization block, internal class, constructor, and enumeration. The constructor is mainly used for quilt calls.
4. Contains abstract methods, or the parent class contains abstract methods but is not implemented. It must be defined as an abstract class.
Abstract: attributes and constructors cannot be modified, static, or private.
Simple template mode rules:
Abstract The parent class only defines some methods that need to be used, and the rest is left for subclass implementation.
The parent class contains methods that need to call other series methods. These calls can be implemented by the parent class or subclass. Some methods provided by the parent class are only common algorithms, its implementation does not completely have its own implementation, but must rely on the assistance of sub-classes.
Interface:
The modifier can be public or the default package access control.
An interface can contain attributes (only constants), methods (only abstract methods), internal classes (including internal interfaces), and enumeration class definitions.
All the Members in the interface are public permissions (or can be omitted without writing), and the attribute in the interface is public static final by default.
A Java source file can have at most one public interface, which is the same as the main file name of the source file.
You can only use the public permission to implement the interface method, because the subclass overrides the parent class method or implements the interface method to have a larger or equal access permission.
Typical application for Interface Programming: simple factory mode and command mode.
Internal class
Functions of internal classes
1. Internal classes provide better encapsulation. Internal classes can be hidden from external classes and cannot be accessed by other classes in the same package.
2. The internal class members can directly access the private data of the external class, while the external class cannot access the implementation details of the internal class.
3. The anonymous internal class is applicable to the classes that only need to be used once.
Internal classes are divided into member Internal classes and local internal classes, while local internal classes are divided into non-static internal classes and static internal classes, the usage of non-static members is similar.
Example:
Class out {
Class in {
Public in (string MSG ){
System. Out. println (MSG );
}
}
}
Public class createinnerinstance {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Out. In in = new out (). New in ("test ");
}
}
Anonymous internal class format
New parent class Constructor (real parameter list) | implementation interface ()
{
// The class part of the anonymous internal class
}
An anonymous internal class must inherit a parent class or implement an interface.
Anonymous internal class usage rules:
1 cannot make abstract classes
2. the constructor cannot be defined because there is no class name, but an initialization block can be defined. You can use the initialization block to complete the constructor's work.
Instance used:
Interface product {
Public double getprice ();
Public String getname ();
}
Public class testanonymous {
/**
* @ Param ARGs
*/
Public void test (product P ){
System. Out. println ("bought a" + P. getname () + ", spent" + P. getprice ());
}
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Testanonymous TA = new testanonymous ();
Ta. Test (New Product ()
{
Public double getprice (){
Return 383.9;
}
Public String getname (){
Return "beauty ";
}
}
);
}
}
Use instance 1 for enumeration classes:
Public Enum seansonenum
{
Spring, summer, falll, winter;
}
Use instance 2 for enumeration classes:
Public Enum gender
{
Mail ("male"), femail ("female ");
Private string name;
Private gender (string name ){
This. Name = Name;
}
Public String getname (){
Return this. Name;
}
}
Abstract class, interface, internal class Summary