Home>Article>Java> What are the similarities and differences between abstract classes and interfaces?

What are the similarities and differences between abstract classes and interfaces?

王林
王林 forward
2020-08-13 16:51:36 2463browse

What are the similarities and differences between abstract classes and interfaces?

The difference between abstract classes and interfaces:

(Recommended tutorial:java introductory tutorial)

Both abstract classes can It contains abstract methods and can also contain non-abstract ordinary methods; the methods in the interface must be abstract methods.

The access type of abstract methods in abstract classes can be public, protected, or default, but abstract methods in interfaces can only be public.

Abstract classes can have ordinary member variables, but all variables in the interface are public static final, that is, they are all constants.

Abstract classes can have constructors, but interfaces cannot have constructors.

Abstract classes can have static methods, but interfaces cannot have static methods.

A class can implement multiple interfaces, but can only inherit one abstract class. If an abstract class implements an interface, the methods in the interface can be mapped to the abstract class as abstract methods without having to implement them, and the methods in the interface can be implemented in subclasses of the abstract class.

The similarities between abstract classes and interfaces:

(Learning video recommendation:java course)

  • Both Can be inherited

  • Can contain method declaration

  • Cannot be instantiated

  • Derived Class must implement unimplemented method

The above is the detailed content of What are the similarities and differences between abstract classes and interfaces?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete