Java abstract classes and interfaces

Abstract class:

· Abstract class is a class specially designed for subclasses to inherit, decorated with abstract .

· Abstract class provides a partial implementation of a class, which can have member variables, construction methods, concrete methods and abstract methods . The abstract method contained in an abstract class ends with a semicolon and does not contain a method body. It is a method that must be overridden by subclasses .

· Format:

abstract class<类名>{
     Member variables;
     Return type method name (parameter list) {method body} //Define general method
     abstract return type method name (parameter list); //Define abstract method
}

· Note:

  (1) An abstract class can only contain abstract methods, or it can have ordinary member variables or methods. Subclasses of abstract classes must override all abstract methods of the superclass.

  (2) If a class is not an abstract class, abstract methods cannot be defined in the class (abstract methods can only be defined in abstract classes).

  (3) An abstract class can have a constructor, but an abstract class will not have an instance, nor can an instance be generated directly by a constructor. Generally, it can be instantiated through its subclasses.

  (4) An abstract method only defines a method declaration, not a method implementation.

  (5) Non-abstract methods can be defined in an abstract class, or abstract methods can be defined.

  (6) For classes, final and abstract cannot be used at the same time; for member methods, static and abstract cannot be used at the same time.

           A class using the final keyword cannot be inherited; a method using the static keyword can be used without instantiating an object, and a method using the abstract keyword has no method body and must be implemented by subclasses.

  (7) abstract does not coexist with final, private and static .

·


interface:

· Interface is a set of protocols established in the early stage of program development (specifying the public operation interface), which is not implemented concretely. The keyword interface specifies what an interface must do, rather than how it should do it. A method defined in an interface has no method body . Once an interface is defined, any class member can implement the interface.

· In the Java language, a class can only have one parent class. But Java provides interfaces for implementing multiple inheritance. A class can have a parent class and implement multiple interfaces . An interface is a special class: it consists of constants and abstract methods .

· Format:

[public] interface interface name [extends parent interface name list]{
     data type variable name = constant value; // constant field declaration
     Return type method name (parameter list) [throw exception list]; //Abstract method declaration
}

· Note:

  (1) Interfaces have inheritance, an interface can also inherit multiple parent interfaces, and the parent interfaces are separated by commas;

  (2) All member variables in the system default interface are decorated with public, static and final.

double PI=3.1415926;
is exactly equivalent to:
public static final double PI=3.1415926;

  (3) The decoration of all member methods in the system default interface is public and abstract.

void testA ();
is exactly equivalent to:
public abstract void testA();

  (4) The interface contains only constant definitions and abstract methods .

  (5) The interface itself has abstract attributes, so there is no need to use the abstract keyword. The access control permissions of the interface have public and default permissions, but do not have protected and private permissions .

  (6) The interface does not contain a constructor .

·


summary:

·An abstract class is an abstraction of the root; an interface is an abstraction of the action.

· Do not define any variables and do not need to give a complete definition of any method, consider the interface;

  Consider abstract classes when there must be concrete method definitions or variables.

·

Guess you like

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