Home  >  Article  >  Java  >  What is the difference between Java abstract classes and interfaces?

What is the difference between Java abstract classes and interfaces?

Guanhui
GuanhuiOriginal
2020-06-08 14:41:112247browse

What is the difference between Java abstract classes and interfaces?

#What is the difference between Java abstract class and interface?

Abstract class: In Java, a class modified by the abstract keyword is called an abstract class, and a method modified by the abstract keyword is called an abstract method. Abstract methods only have methods. Statement, there is no method body. Characteristics of abstract classes:

1. Abstract classes cannot be instantiated and can only be inherited;

2. Those that contain abstract methods must be abstract classes, but abstract classes do not necessarily contain abstract methods;

3. The modifier of an abstract method in an abstract class can only be public or protected, and the default is public;

4. If a subclass inherits an abstract class, the subclass must implement the parent Class abstract method, otherwise the subclass must also be defined as an abstract class;

5. Abstract classes can contain attributes, methods, and constructors, but constructors cannot be used for instantiation and are mainly used to be called by subclasses.

Interface: The interface in Java is modified with the interface keyword. The characteristics are:

1. The interface can contain variables and methods; the variable is designated as public static final by Hermit. The method is designated as public abstract by Hermit (before JDK1.8);

2. The interface supports multiple inheritance, that is, one interface can extend multiple interfaces, which indirectly solves the problem of single inheritance of classes in Java;

3. A class can implement multiple interfaces;

4. New features have been added to interfaces in JDK1.8:

  • Default method (default method): JDK 1.8 allows adding non-abstract method implementations to interfaces, but they must be modified with the default keyword; the method that defines default does not need to be implemented by the implementation subclass, but can only be called by the object that implements the subclass; if the subclass If a class implements multiple interfaces, and these interfaces contain the same default method, the subclass must override the default method;

  • static method: static method is allowed in JDK 1.8 The keyword modifies a method and provides an implementation, which is called an interface static method. Interface static methods can only be called through the interface (interface name. static method name).

Recommended tutorial: "PHP Tutorial"

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

Statement:
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