In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what are the two differences between abstract classes and interfaces in Java. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
What's the difference between abstract classes and interfaces? Under what circumstances do you use abstract classes? Under what circumstances do you use an interface?
The difference between abstract classes and interfaces:
1. Abstract functions are methods modified with the abstract keyword, and abstract classes are classes that contain abstract functions or are defined using abstract class.
Abstract class is mainly used to abstract the common properties and methods of a group of subclasses. abstract classes can not be instantiated, abstract classes can only be inherited as parent classes, and abstract classes can be used as templates for subclasses.
two。 Interfaces are collections of abstract methods, and interfaces are usually declared as interface.
Class implements the interface, thus inheriting the abstract methods in the interface. When implementing an interface, you need to implement all the methods in the interface.
Design level differences:
Abstract classes can define common methods, and if the abstract class adds a method, the subclass will automatically have the corresponding function; while if a method definition is added to the interface, then all implementation classes need to define their own implementation.
Abstract class emphasizes methods and attributes, abstracting the common methods and attributes, corresponding to the concept of is a, which kind of subclass is; the interface emphasizes behavior, highlighting the function of the class that implements the interface, and the concept of corresponding has a.
The interface is used to implement multi-inheritance, decoupling and code reuse in C++.
When do you use abstract classes and interfaces?
When there are many methods and you want some of them to have a default implementation, use abstract classes. Multiple inheritance is not supported in Java, and a class can inherit only one parent class, but a class can implement multiple interfaces, which need to be used in the case of multiple inheritance. If the underlying contract is constantly changing, then abstract classes should be used. Because if your underlying contract changes all the time, and you still use the interface, you must change all the classes that implement the interface each time the contract changes.
On the abstract classes and interfaces in Java what are the two differences are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.