Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use abstract and interface in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use abstract and interface in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction

Abstract and interface keywords can be seen everywhere in Java, and they are one of the important pillars of the implementation of the three major features of Java: encapsulation, inheritance and polymorphism. The interface keyword is used to define the interface abstraction, which is essentially used to define the type and the capabilities of the class. But novices often mistakenly use abstract and interface, and LXXXVI also makes mistakes. In this article, we focus on the use of interface interface and abstract abstract classes.

It is recommended to read with two questions before the article begins:

What's the difference between abstract and interface?

How should I choose abstract and interface?

2. Criteria

When defining interfaces, there are some guidelines to refer to, according to which you can better determine whether you should define interfaces, or whether there are other better alternatives. Note that the point made by Xiaogu is not absolutely correct, it should be analyzed in detail in the actual development process, and if there is something wrong, we can communicate with each other. )

2.1 Interface takes precedence over abstract class

Xiaoqi uses the inheritance system of JDK's source code HashMap to illustrate that interfaces take precedence over abstract classes.

HashMap inheritance architecture class diagram structure:

Top-level interface of HashMap:

Public interface Map {}

Abstract classes implemented by HashMap:

Public abstract class AbstractMap implements Map {}

You can see that HashMap inherits the AbstractMap abstract class and implements the Map interface, but why do interfaces take precedence over abstract classes? These are because Java is a single inheritance multi-implementation, HashMap can not inherit other classes after inheriting the AbstractMap abstract class, if it is an interface, there is no this restriction, for example, HashMap also needs to provide serialization and cloning functions, HashMap can implement the three interfaces Map, Cloneable, Serializable.

In that case, why does HashMap inherit the AbstractMap abstract class?

This is because in the JDK source code design, the Map structure JDK needs to provide the default implementation of some methods, so the authors of JDK pulled an abstract class separately to implement these methods; although Java8 Oracle tried to provide static methods and common methods in the interface, Xiao Hsiao thought that it was not to a certain degree of requirement, and that the method implementation should not be defined in the interface as far as possible.

What's the difference between abstract and interface?

In fact, the difference continues to narrow after Java8, but on the whole, it is still two completely different concepts:

The characteristics of abstract class abstract:

Both abstract methods and abstract classes must be modified by the abstract keyword

If there are abstract methods in a class, then the class must be an abstract class

There may not be abstract methods in an abstract class

Constructors can exist in abstract classes

There can be ordinary properties, methods, static properties, and static methods in abstract classes

The methods of an abstract class must be implemented in a subclass, otherwise the subclass also needs to be defined as an abstract class

Abstract classes cannot create objects with new, because calling abstract methods is meaningless without implementation

Features of interface interface:

The methods in the interface are all modified by public

There is no constructor in the interface, and the interface object cannot be instantiated

There are only constants in the interface. If a variable is defined, public static final is added by default.

Multiple inheritance can be achieved by using interfaces

There is only the declaration of the method in the interface, but there is no method body (before Java8, when I didn't say it, but many people think so, this wrong idea often can design the code correctly)

Static methods can be declared in the interface and must be public decorated (default). Static methods cannot be overridden by subclasses.

Common methods can be declared in the interface, which must be default decorated

Comparison Abstract Class (abstract) Interface (interface) multiple inheritance is not supported (only one abstract class can be inherited) support (class can implement many interfaces) method abstract class can contain both abstract and non-abstract method interfaces all methods implicitly abstract (Java can define static methods) whether the constructor allows instantiation can not be instantiated can not be instantiated access modifier abstract class can use public, default Abstract methods can use public, default, protected; ordinary methods can use public, default, protected, private interfaces can use public, default; methods default public

Summary:

Throughout the abstract implementation architecture, default implementations of some methods must be provided, and abstract classes can be used (because it is highly discouraged to implement some methods directly in the interface)

If you do not need to provide a default implementation, and you need to implement multi-inheritance, use the interface

2.2 methods should not be implemented in the interface

Interface is everywhere, interface as the top level of class architecture, all the constraints and specifications provided by interface directly affect the lower-level implementation class. Therefore, it is not recommended to implement specific methods in the interface, although the interface definition after Java8 can provide static method implementation and common method implementation, but this implementation is very risky, unless your interface design is perfect enough to be responsible for all the implementation classes to say that your logic will never change. Otherwise, the following classes that use this method will suffer after the logic of the specific implementation method of the interface is modified.

Therefore, interfaces are only used to define types and define the capabilities of classes as far as possible. If you have to define an implementation, consider using abstract classes to define it.

2.3 Interface should not be used to export constants

Because it is very convenient to define constants in an interface, some partners use the interface to export classes directly as constants, such as the following:

/ *

* cache key *

* * @ Author: Liziba * @ Date: 23:12 on 2021-11-2 * / public interface CacheKey {String USER = "user"; String ORDER = "order"; String MAIL = "mail";}

Although it looks very simple, there is no problem with its use. But the problem is that the interface is not used to export constants for you. If we need to define constants, we can use enumerations or constant classes.

For example, this method is as follows:

Public class CacheKey {public static final String USER = "user"; public static final String ORDER = "order"; public static final String MAIL = "mail";}

Note that Xiaoqi is talking about not using interfaces to export classes only as constants, not that you can't define constants in interfaces. If some constants are used uniformly in class abstract types, you can consider this design (but it is not recommended! It is often better to extract constant classes to manage these constants separately

That's all for "how to use abstract and interface in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report