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

What is the difference between abstract class and interface in java

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

Share

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

This article introduces the relevant knowledge of "what is the difference between abstract class 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!

Let's compare the grammatical differences between the two:

1. Abstract classes can have constructors, and interfaces cannot have constructors.

two。 There can be ordinary member variables in an abstract class, and there are no ordinary member variables in the interface.

3. An abstract class can contain non-abstract ordinary methods, and all methods in an interface must be abstract, and there can be no non-abstract ordinary methods.

4. The access types of abstract methods in abstract classes can be public,protected and (the default type, although no errors are reported under eclipse, should not work), but abstract methods in interfaces can only be of type public, and the default is public abstract type.

5. Abstract classes can contain static methods, and interfaces cannot contain static methods

6. Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but the variables defined in the interface can only be public static final types, and the default is public static final type.

7. A class can implement multiple interfaces, but can only inherit one abstract class.

Next, let's talk about the difference between the two in application:

The interface plays a more important role in the system architecture design method, which is mainly used to define the communication contract between modules. Abstract classes play a role in code implementation and can achieve code reuse. For example, the template method design pattern is a typical application of abstract classes. Assuming that all Servlet classes of a project have to judge permissions, record access logs and handle exceptions in the same way, an abstract base class can be defined so that all Servlet inherits this abstract base class. The permission determination, access log and exception handling code are completed in the service method of the abstract base class, and the business logic code is only completed in each subclass. The pseudo code is as follows:

Public abstract class BaseServlet extends HttpServlet

{

Public final void service (HttpServletRequest request

HttpServletResponse response) throws

IOExcetion,ServletException

{

Record access log

To determine the authority

If (with permissions)

{

Try

{

DoService (request,response)

}

Catch (Excetpion e)

{

Record abnormal information

}

}

}

Protected abstract void doService (HttpServletRequest

Request, HttpServletResponse response) throws

IOExcetion,ServletException

/ / Note that the access permission is defined as protected, which is both professional and rigorous, because it is dedicated to subclasses.

}

Public class MyServlet1 extends BaseServlet

{

Protected void doService (HttpServletRequest request

HttpServletResponse response) throws

IOExcetion,ServletException

{

Specific business logic code that this Servlet only deals with

}

}

A piece of code in the middle of the parent method is uncertain, leaving it to the subclass, and the template method is used to design the pattern.

This is the end of the content of "what's the difference between abstract class and interface in java". Thank you for your 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