
#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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft





