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 distinguish between abstract classes and interfaces in Java

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Java abstract classes and interfaces how to distinguish the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will gain after reading this Java abstract classes and interfaces how to distinguish the article, let's take a look.

What are abstract classes and interfaces? What's the difference?

There may be some differences in the way interfaces and abstract classes are defined by different programming languages, but not much. This article uses the Java language.

Abstract class

Let's look at a typical usage scenario of an abstract class through an example.

Logger is an abstract class for logging. FileLogger and MessageQueueLogger inherit Logger and implement two different logging methods:

Log to a file

Log to message queue

The FileLogger and MessageQueuLogger subclasses reuse the name, enabled, minPermittedLevel properties and log methods in the parent class Logger, but because the two subclasses write logs in different ways, they each override the doLog method in the parent class.

Parent class

Import java.util.logging.Level;/** * Abstract parent Class * @ author yanliang * @ date 9 Universe 27 Universe 27 5:59 PM * / public abstract class Logger {private String name; private boolean enabled; private Level minPermittedLevel; public Logger (String name, boolean enabled, Level minPermittedLevel) {this.name = name; this.enabled = enabled; this.minPermittedLevel = minPermittedLevel } public void log (Level level, String message) {boolean loggable = enabled & & (minPermittedLevel.intValue ()

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