
The difference between interface and abstract class
There is a big difference between interface and abstract class, and the choice of them even reflects Whether the understanding of the nature of the problem domain and the design intention is correct and reasonable.
|
Abstract class |
Interface |
Instantiation |
Cannot |
Cannot |
Class |
An inheritance relationship, a class can only use the inheritance relationship once. Multiple inheritance can be achieved by inheriting multiple interfaces |
A class can implement multiple interfaces |
Data members |
Can have its own |
Static cannot be modified, that is, it must be static final, generally not defined here |
Method |
Can be private, non-abstract method, must be implemented |
cannot be private, default Is public, abstract type |
Variable |
can be private, the default is friendly type, its value can be in It can be redefined in a subclass or reassigned. |
cannot be private. The default is public static final type, and its initial value must be given. It cannot be redefined or changed in the implementation class. its value. |
Design concept |
represents the "is-a" relationship |
represents the "like-a" relationship |
To achieve |
requires inheritance, use extends |
To use implementations |
1. Similarities
A. Both are abstract classes and cannot be instantiated.
B. Both interface implementation classes and subclasses of abstract class must implement the declared abstract methods.
Recommended learning: java tutorial
2. Differences
A. The interface needs to be implemented, and implements must be used. Abstract class needs to be inherited and uses extends.
B. A class can implement multiple interfaces, but a class can only inherit one abstract class.
C. Interface emphasizes the implementation of specific functions, while abstract class emphasizes ownership relationships.
D. Although both the interface implementation class and the subclasses of abstract class must implement the corresponding abstract methods, the implementation forms are different. Every method in the interface is an abstract method, which is only declared (declaration, without method body), and the implementation class must implement it. Subclasses of abstract class can be implemented selectively.
3. Application occasions of interface
A. Classes need specific interfaces for coordination, regardless of how they are implemented.
B. It exists as an identifier that can realize a specific function, or it can be a pure identifier without any interface methods.
C. A group of classes needs to be treated as a single class, and the caller only contacts this group of classes through the interface.
D. Specific multiple functions need to be implemented, and these functions may have no connection at all.
4. Application occasions of abstract class
In a word, when both a unified interface and instance variables or default methods are needed, then Can use it. The most common ones are:
A. Define a set of interfaces, but do not want to force each implementation class to implement all interfaces. You can use abstract class to define a set of method bodies, or even empty method bodies, and then let subclasses choose the methods they are interested in to cover.
B. In some cases, pure interfaces alone cannot satisfy the coordination between classes, and variables representing states in the class are also required to distinguish different relationships. The intermediary role of abstract can satisfy this very well.
C. Standardizes a set of mutually coordinated methods, some of which are common, independent of state, and can be shared without the need for subclasses to implement them separately; while other methods require each subclass to implement it according to its own A specific state to achieve a specific function.
For more programming tutorials, please continue to pay attention to the PHP Chinese website.
The above is the detailed content of The difference between interface and abstract class. For more information, please follow other related articles on the PHP Chinese website!
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PMThe article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.
How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PMThe article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PMThe article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra
How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PMThe article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]
How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PMJava's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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),






