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 abstract classes and interfaces in java define how to use the

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

Share

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

Xiaobian to share with you how to define and use abstract classes and interfaces in java, I believe most people still do not know how to use, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

1. What is abstract class?

For example, in life we all classify dogs and cats as animals, but when we only say animals, we don't know whether it is cats or dogs or something else. So animals are abstract classes, cats and dogs are concrete classes. Therefore, in Java, a method without a method body should be defined as an abstract class, and a class with abstract methods must be an abstract class.

2. Features of abstract classes:

Abstract classes and abstract methods must be qualified with the abstract keyword.

Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes.

Abstract classes cannot be instantiated. If instantiation is required, refer to the polymorphic form and instantiate them through subclasses.

Subclasses that inherit abstract classes need to override all abstract methods in the abstract class, or abstract classes.

3. Characteristics of Members:

Member variables: can be variables or constants.

Constructor method: There are constructor methods, but they cannot be instantiated, so they need to be carried out through polymorphic subclasses; the use is to access the parent class for data initialization.

Member methods: can be abstract classes: limit certain behaviors that subclasses must perform; can also be non-abstract classes, which can improve code reusability.

1. What is the interface?

An interface is a common specification standard, an additional condition required to represent a class, so it can be used only if it meets the specification standard. Interfaces in Java are mainly embodied in abstractions of behavior.

2. Features of the interface

You need to use the keyword interface.

The implementation interface of a class needs to be represented by implements

An interface cannot be instantiated. To instantiate, it is instantiated by referring to a polymorphic form, which is called interface polymorphism. Therefore, the main forms of polymorphism are: specific class polymorphism, abstract polymorphism, interface polymorphism.

Implementing classes of interfaces: either override abstract methods in interfaces or abstract classes.

3. Component members of an interface

1. Member variables: can only be constants, modified by 'public static final' by default.

2, construction method: interface has no construction method, because the interface is mainly abstract for behavior, there is no concrete existence.

Note: A class without a parent class inherits the Object class by default.

3. Member Method:

Abstract Methods: The default methods of an interface are abstract methods. The default is public static abstract.

Default method: The default method is the method used when some methods need to be added to the implementation class. It needs to be modified with default in the interface. It can be overridden in the implementation class, but it needs to be deleted when overriding.

Static methods: static methods can only be added to the interface, cannot be called by the implementation class, can only be called by the interface name and be static.

Private methods: private methods are mainly used in interfaces, decorated with private keywords, by writing the same code segment in private methods, improve the code reuse and simplicity, static private methods can only be decorated with static, non-static can have non-static and static methods can be decorated.

4. Relationship between class and abstraction:

5. Difference between abstract classes and interfaces:

Abstract classes are abstractions of things, and interfaces are abstractions of behaviors.

The above is "java abstract classes and interfaces how to define the use of" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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