Interfaces and Abstract Classes

  • 1. Implement the interface with the implements keyword


  • 2. How to create an abstract class: abstract class className{ }


  • 3. Intrface is commonly decorated with public, and the system will add abstract by default;


  • 4. The attributes in the interface are constants. Even if the public static final modifier is not added when the definition is made, the system will automatically add it;


  • 5. When overriding the method of the interface, the method in the interface must be a public abstract method, so when a class inherits the interface and rewrites the interface method, it must not only remove the abstract modifier, give the method body, but also clearly identify it with public out his access rights;


  • 6. Classes modified by abstract cannot create objects and can only inherit;

    • abstract cannot modify constructors and static methods;


  • 7. About abstract classes and interfaces:

    • Classes cannot have multiple inheritance but interfaces can;

    • Neither abstract classes nor interfaces can be instantiated, that is, abstract classes cannot be instantiated through new;

    • Abstract classes can contain properties, methods, and constructors. However, the constructor cannot be used for new instances, it can only be used to be called by subclasses;

    • Abstract classes can only be used for inheritance;

    • Abstract methods must be implemented by subclasses.

    • There are only: constants, abstract methods in the interface

    • Interfaces can inherit interfaces (through extends), and multiple inheritance is possible


  • 8. About abstract classes:

    • A subclass can only inherit one abstract class, but can implement multiple interfaces;

    • Abstract classes can have constructors, interfaces have no constructors;

    • An abstract class can have ordinary member variables, but an interface cannot have ordinary member variables;

    • Both abstract classes and interfaces can have static member variables, the access rights of static member variables in abstract classes are arbitrary, and interfaces can only be public static final (default);

    • Abstract classes can have no abstract methods, abstract classes can have ordinary methods, and interfaces are all abstract methods;

    • Abstract classes can have static methods, but interfaces cannot have static methods;

    • Methods in abstract classes can be public or protected; interface methods are only public


  • 9. Abstract classes may not contain abstract methods, but classes with abstract methods must be declared as abstract classes;

 abstract class abstclass{
    abstract void meth();
}

  • 10. No methods and member variables can be declared in the interface:

interface testinterface{
    
}

  • 11. Abstract classes and interfaces:

Similarities: both cannot be instantiated, are at the top of the inheritance tree, and both contain abstract methods

difference:

    • 1. Design purpose: A specification embodied by the interface, similar to the general outline of the entire system, and formulates the standards that each module of the system should follow. Therefore, the interface should not be changed frequently, and once the change is radiative to the entire system. As the common parent class of multiple subclasses, the abstract class embodies a template design, which can be used as an intermediate product in the system implementation process, and has already realized some functions of the system.
    • 2. Use different:
      • (1) An interface can only contain abstract methods, and an abstract class can contain ordinary methods.
      • (2) Static methods cannot be defined in interfaces, but abstract classes can.
      • (3) Interfaces can only define static constant attributes but not ordinary attributes, but abstract classes can.
      • (4) Interfaces do not contain constructors, abstract classes can (not for creating objects but for subclasses to complete initialization).
      • (5) An interface cannot contain an initialization block, an abstract class can.
      • (6) Multiple inheritance of interfaces, abstract classes but inheritance (there can only be one direct parent class).

Summary: All methods of the interface are abstract and can only be modified by public abstract (default public abstract modification), and properties are modified by default public static final. An abstract class is indistinguishable from a normal class except that it contains abstract methods.


  • 12. Abstract classes and interfaces

  • 1. An abstract class can have a constructor, but an interface cannot have a constructor.

  • 2. There can be ordinary member variables in the abstract class, but there are no ordinary member variables in the interface

  • 3. An abstract class can contain non-abstract ordinary methods, all methods in the interface must be abstract, and there cannot be non-abstract ordinary methods.

    1. The access type of the abstract method in the abstract class can be public, protected and (the default type, although
      no error is reported under eclipse, but it should not work), but the abstract method in the interface can only be of the public type, and the default is public abstract Types of.
    1. Abstract classes can contain static methods, but interfaces cannot contain static methods
    1. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but variables defined in interfaces can only be of public static final type, and the default is public static final type.

Guess you like

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