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

王林
Release: 2020-08-13 16:51:36
forward
2439 people have browsed it

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!