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

Default method format of Java interface and how to use it

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "Java interface default method format and how to use", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java interface default method format and how to use" article.

1. Format of the default method in the API: public default returns the value type, method name (parameter list) {} 2, and notes on the default method in the API:

The default method is not abstract, so it is not overridden. But you can rewrite, and when you rewrite, you can delete keywords.

Public can be omitted, but default cannot be omitted.

3. Examples

1: define an interface MyInterface with two abstract methods:

Void show1 ()

Void show2 ()

2: define two implementation classes of the interface:

MyInterface ImplOne

MyInterface ImplTwo

3: define the test class:

MyInterfaceDemo

In the main method, you create an object in a polymorphic manner and use the

Expansion of knowledge points:

What is an interface (interface overview)

Since the first thing to use an interface is to make clear what an interface is, and what it can help us achieve.

An interface is a specification, which defines a set of rules that reflect the real world "if you are... then you have to be able to..." The mind of. For example, if you are a car, you must be able to run; if you are a bad person, you must bully the good guys.

Interfaces are "abstract" classes that are more abstract than "abstract classes" and can be more standardized to constrain subclasses. Comprehensively and professionally realized: the separation of specification and concrete implementation.

An interface is a reference type in the Java language and a "collection" of methods, so the interior of the interface is mainly defined methods, including constants, abstract methods (JDK 7 and before).

The above is about the content of this article on "the default method format of Java interface and how to use it". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow 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