search
HomeJavaJavagetting StartedWhat is the difference between Java abstract classes and interfaces?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use