Java abstract class interface

1. abstract class

  In the java language, when a class is modified with the abstract keyword, the class is called an abstract class, and when a method is modified with the abstract keyword, the method is called an abstract method. The format is as follows:
  abstract class abstractClass{ …} //abstract class
  abstract returnType abstractMethod([paramlist]) //abstract method

 Abstract classes must be inherited and abstract methods must be overridden. Abstract methods only need to be declared, not implemented; abstract classes cannot be instantiated, and abstract classes do not necessarily contain abstract methods. If a class contains abstract methods, the class must be defined as abstract.

 2. interface

  An interface is a type of abstract class, which only contains the definitions of constants and methods, without the implementation of variables and methods, and its methods are all abstract methods. Its usefulness is reflected in the following aspects:
  ◇ To achieve the same behavior of unrelated classes through interfaces, without considering the relationship between these classes.
  ◇ Specify the methods that multiple classes need to implement through the interface.
  ◇ Understand the interactive interface of the object through the interface, without knowing the corresponding class of the object.

  1) Definition of
  an interface The definition of an interface includes an interface declaration and an interface body.
  The format of an interface declaration is as follows:
  [public] interface interfaceName[extends listOfSuperInterface] { … }
   The extends clause is basically the same as the extends clause of a class declaration, except that an interface can have multiple parent interfaces, separated by commas, and one A class can only have one parent class.

  The interface body includes constant definitions and method definitions. The
  constant definition format is: type NAME=value; the constant is shared by multiple classes that implement the interface; it has public, final, and static attributes.
  The method body definition format is: (with public and abstract attributes)
  returnType methodName([paramlist]);

  2) The implementation of the interface The implements clause is used
  in the class declaration to indicate that a class uses an interface, the constants defined in the interface can be used in the class body, and all methods defined in the interface must be implemented. A class can implement multiple interfaces, separated by commas in the implements clause.

  3) The use of interface types The
  interface used as a reference type. Instances of any class that implements the interface can be stored in variables of the interface type, and through these variables you can access methods in the interface that the class implements.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325898490&siteId=291194637