The difference between ordinary class, abstract class and interface

1. The difference between ordinary class, abstract class and interface:

1. Ordinary classes can be instantiated, and interfaces cannot be instantiated (it has no construction method). If an abstract class is to be instantiated, the abstract class must point to a subclass object that implements all abstract methods (abstract classes can be instantiated directly, and re Write your own abstract method), the interface must point to the class object that implements all interface methods.

2. Abstract classes must be inherited by subclasses, and interfaces must be implemented by subclasses.

3. Interfaces can only be used for method declarations, and abstract classes can be used for method declarations and method implementations.

4. The variables defined in the interface can only be public static constants, and the variables defined in the abstract class are ordinary variables.

5. The abstract methods in the abstract class must all be implemented by the subclass. If the subclass cannot fully implement the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when an interface is implemented, if all interface methods cannot be implemented, the class can only be an abstract class.

6. Abstract methods can only be declared but cannot be implemented. The interface is the result of design, and the abstract class is the result of refactoring.

7. There can be no abstract methods in abstract classes.

8. If there are abstract methods in a class, then the class can only be an abstract class.

9. Abstract methods must be implemented, so they cannot be static or private.

10. The interface can inherit the interface, and can inherit multiple interfaces, but the class can only inherit single. (Important)

11. Constants in the interface: there is a fixed modifier-public static final (cannot be modified with private and protected/essentially all static and final type, regardless of whether static modification is added).

12. Abstract methods in the interface: there is a fixed modifier-public abstract.

13. Interface details:

If the method or variable in the interface is not written as public, static, final / public, abstract, it will be filled in automatically.

The members in the interface are shared.

There is an inheritance relationship between interfaces and interfaces, and multiple inheritance is possible.

Interface cannot be instantiated

A class can implement multiple interfaces

In java development, we often define commonly used variables in interfaces and use them as global variables. The access form is: interface name.variable name.

An interface cannot inherit other classes, but it can inherit other interfaces

An important principle: When a class implements an interface, the class is required to implement all the methods of this interface

note:

① Abstract classes and interfaces are used to abstract concrete objects, but the abstraction level of the interface is higher.

② Abstract classes can have concrete methods and attributes, while interfaces can only have abstract methods and static constants.

③ Abstract classes are mainly used for abstraction levels, and interfaces are mainly used for abstract functions.

④ In the abstract class, and does not contain any realization, the derived class must cover them. All methods in the interface must be unimplemented.

⑤ For interface methods, the access authority must be public.

⑥ There can only be public methods in the interface, and no member variables.

⑦ The interface can only contain unimplemented methods, also called abstract methods, but the abstract keyword cannot be used.

⑧ The access speed of abstract classes is faster than that of interfaces. Interfaces are slightly slower because it takes time to find the methods implemented in the class.

⑨ Abstract classes, except that they cannot be instantiated, are no different from ordinary java classes.

⑩ An abstract class can have a main method, but an interface does not have a main method.

⑪ Abstract classes can use constructors, but interfaces do not.

⑫ Abstract methods can have public, protected and default modifiers, and interfaces can only use the default public.

⑬ Abstract class, adding new methods can provide default implementation without changing the original code. To add new methods to the interface, the subclass must implement it.

⑭ Subclasses of abstract classes are inherited by the extends keyword, and interfaces are realized by implements.

 

2. When to use abstract classes and interfaces

1. If you have some methods and want some of them to have default implementations, use abstract classes.

2. If you want to implement multiple inheritance, you must use interfaces. Since Java does not support multiple inheritance, subclasses cannot inherit multiple parent classes, but can implement multiple interfaces, so you can use interfaces to implement it.

3. If the basic basic functions are constantly changing, then you need to use abstract classes. If you continue to change the basic functions and use interfaces, then all implementation classes need to be changed.

 

Guess you like

Origin blog.csdn.net/weixin_37081112/article/details/103999108