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 are the differences between java abstract classes and interfaces

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the differences between java abstract classes and interfaces". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the differences between java abstract classes and interfaces".

Part of it comes from: the difference between Java abstract classes and interfaces-ImportNew

Abstract Class-- A template Design

Abstract void fun (); [public] abstract class ClassName {abstract void fun ();}

Abstract classes exist for inheritance.

An abstract class cannot be instantiated, instantiation is left to its subclass, it only needs a reference.

Abstract methods are overridden by subclasses. Must be public or protected. Default is public.

As long as it contains an abstract class of an abstract method, the method must be defined as an abstract class, whether or not it contains other methods.

Abstract classes can contain concrete methods or, of course, no abstract methods.

Abstract cannot decorate the same class side by side with final.

Abstract cannot modify the same method side by side with private, static, final, or native.

The meaning of an abstract class is:

Provide a common parent type for its subclass to prevent the class from being instantiated

Encapsulate duplicates in subclasses (member variables and methods)

Define public abstract methods, with different implementations provided by subclasses.

Interface-A radiant design (a code of conduct) [public] interface InterfaceName {}

The interface itself is also a class

Variable is implicitly specified as public static final (can only be public static final variable, and compilation error will be reported with private or private modifier)

Methods [must all abstract methods] are implicitly specified as public abstract (can only be public abstract methods)

All methods in an interface cannot have a specific implementation (there can be default and static methods in jdk1.8)

An interface is an extremely abstract type, more "abstract" than an abstract class, and generally does not define variables in the interface (if the definition can only be Static Final)

Contrast--

Grammatical differences

1) Abstract classes can provide implementation details of member methods, but only public abstract methods can exist in interfaces.

2) member variables in an abstract class can be of various types, while member variables in an interface can only be of type public static final.

3) the interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods.

4) A class can inherit only one abstract class, while a class can implement multiple interfaces.

Differences at the design level

What is template design? Example: using template A to design ppt B and ppt C, the common part of ppt B and ppt C is template A. if the common part needs to be changed, only template A needs to be changed, and there is no need to change ppt B and ppt C.

What is radiation design? For example, an elevator is equipped with some kind of alarm, once you want to update the alarm, you must update it all.

Abstract classes are abstracted from the bottom up, and interfaces are designed from top to bottom.

For an abstract class, if you need to add a new method, you can add a concrete implementation directly to the abstract class, and the subclass can not be changed.

Not for the interface, if the interface has been changed, then all classes that implement the interface must change accordingly.

Comparison of abstract classes and interfaces

Parameter abstract class interface default method implementation it can have a default method implementation interface is completely abstract. There is no method implementation subclass that uses the extends keyword to inherit abstract classes. If the subclass is not an abstract class, it needs to provide an implementation of all declared methods in the abstract class. The subclass implements the interface using the keyword implements. It needs to provide all the methods declared in the interface of the implementation constructor abstract class can have a constructor interface can not have the difference between the constructor and the normal Java class, except that you can not instantiate the abstract class, it and the ordinary Java class there is no difference interface is completely different type access modifiers abstract methods can have public, protected and default these modifiers interface methods default modifier is public. You cannot use other modifiers. The main method abstract method can have a main method and we can run it the interface does not have a main method, so we cannot run it. A multi-inheritance abstract method can inherit a class and implement multiple interfaces. It can only inherit one or more other interfaces. It is faster than the interface. The interface is slightly slower because it takes time to find the methods implemented in the class. Add a new method if you add a new method to an abstract class, you can provide a default implementation. So you don't need to change your current code. If you add a method to an interface, you must change the class that implements the interface. When to use abstract classes and interfaces

If you have some methods and want some of them to have default implementations, use abstract classes.

If you want to implement multiple inheritance, then you must use the interface. Because Java does not support multiple inheritance, subclasses cannot inherit multiple classes, but can implement multiple interfaces. So you can use the interface to solve it.

If the basic functionality is constantly changing, you need to use abstract classes. If you keep changing the basic functionality and using the interface, you need to change all the classes that implement the interface.

Example--

Examples of doors and alarms: both doors have open () and close () actions, and we can define this abstract concept through abstract classes and interfaces:

Abstract class Door {public abstract void open (); public abstract void close ();}

Or:

Interface Door {public abstract void open (); public abstract void close ();}

This approach violates ISP (Interface Segregation Principle), a core principle of object-oriented design [see note], and mixes the behavior inherent in the concept of Door with the behavior side method of another concept, "alarm", in the definition of Door. One problem that arises is that modules that rely solely on the concept of Door will change because of the change in the concept of "alarm", and vice versa.

Door's open (), close () and alarm () belong to two different categories of behavior. Open () and close () belong to the inherent behavior characteristics of the door, while alarm () belongs to the extended additional behavior. Therefore, the best solution is to design the alarm as a separate interface, including alarm () behavior, and Door as a separate abstract class, including open and close behavior. Then design an alarm door to inherit the Door class and implement the Alarm interface.

Interface Alram {void alarm ();} abstract class Door {void open (); void close ();} class AlarmDoor extends Door implements Alarm {void oepn () {/ /.... } void close () {/ /.... } void alarm () {/ /.... }} Note:

ISP (Interface Segregation Principle Interface Separation principle): a core principle of object-oriented. It shows that it is better to use multiple specialized interfaces than to use a single master interface.

The dependency of one class on another should be based on the smallest interface.

An interface represents a role, and different roles should not be assigned to one interface. Unrelated interfaces are merged together to form a bloated large interface, which is a pollution to roles and interfaces.

Thank you for your reading, the above is the content of "what is the difference between java abstract class and interface". After the study of this article, I believe you have a deeper understanding of the difference between java abstract class and interface, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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