In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what is the difference between abstract class and interface in Java development". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what's the difference between abstract class and interface in Java development?"
What is the difference between abstract class and interface in Java development?
Class with the abstract modifier is an abstract class, an instance object that cannot be created by the abstract class. Classes that contain abstract methods must be defined as methods in the abstract class,abstract class class that do not have to be abstract. Abstract methods defined in abstract class classes must be implemented in Concrete subclasses, so there can be no abstract constructors or abstract state methods. If your subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type.
An interface (interface) can be described as a special case of an abstract class, and all methods in the interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final. 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:
A certain piece of code in the middle of the parent method is uncertain, leaving it to the subclass to dry, and the template method is used to design the pattern.
At this point, I believe you have a deeper understanding of "what is the difference between abstract class and interface in Java development?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.