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 to implement the update of java interface composition

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to update the composition of java interface". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to update the composition of java interface" can help you solve the problem.

1.1 Overview of interface composition update

Composition of the interface

Constant: public static final

Abstract method: public abstract

Default method (Java 8)

Static method (Java 8)

Private methods (Java 9)

1.2 default method in the interface (JDK8)

As we all know, if a class implements an interface, it is necessary to override all abstract methods in that interface. But now that a new method has been added to the interface, what if the class that implements the interface doesn't want to override this method? At this point, the default method in the interface can be used, which is not forced to be overridden, and the method body can be provided.

The definition format of the default method in the interface:

Format: public default returns value type method name (parameter list) {}

Example: public default void show () {}

Considerations for the default method in the interface:

The default method is not abstract, so it is not forced to be overridden. But it can be rewritten, removing the default keyword when rewriting

Public can be omitted, but default cannot be omitted: default void show () {}

1.3static methods in the interface (JDK8)

The definition format of static methods in the interface:

Format: public static returns value type method name (parameter list) {}

Example: public static void show () {}

Considerations for static methods in the interface:

Static methods can only be called through the interface name, not by implementing the class name or object name.

Public can be omitted, but static cannot be omitted: static void show () {}

Interface

Package test;public interface Inter {void show (); default void method () {System.out.println (default method);} / / public static void test () {/ / System.out.println (static method); / /} static void test () {System.out.println (static method);}}

Implementation class

Package test;public class InterImpl implements Inter {@ Override public void show () {System.out.println ("show method");}}

Test class

Package test;public class Demo {public static void main (String [] args) {Inter I = new InterImpl (); i.show (); / show method i.method (); / i.test (); / / error Inter.test (); / / static method. The interface name calls the private method (JDK9) in the interface.

Private methods with method bodies are added in Java 9, which foreshadows Java 8: Java 8 allows default and static methods with square bodies to be defined in the interface. This may give rise to a problem: when two default or static methods contain the same code implementation, the program must consider extracting the implementation code into a common method that does not need to be used by others, so it is hidden by private methods, which is the inevitability of Java 9 to increase private methods.

The definition format of private methods in the interface:

Format 1 (non-static): private returns the value type method name (parameter list) {}

Example 1:private void show () {}

Format 2 (static): private static returns the value type method name (parameter list) {}

Example 2:private static void method () {}

Considerations for private methods in the interface:

Default methods can call private static and non-static methods

Static methods can only call private static methods

Package test;public interface Inter {default void show1 () {System.out.println ("show1 starts"); / / System.out.println ("Junior engineer"); / / System.out.println ("Intermediate engineer"); / / System.out.println ("Senior engineer"); / / show (); method () System.out.println ("show1 end");} static void method1 () {System.out.println ("method1 starts execution"); / / System.out.println ("junior engineer"); / / System.out.println ("intermediate engineer"); / / System.out.println ("senior engineer"); method () System.out.println ("method1 end");} private void show () {System.out.println ("junior engineer"); System.out.println ("intermediate engineer"); System.out.println ("senior engineer");} private static void method () {System.out.println ("junior engineer") System.out.println ("Intermediate engineer"); System.out.println ("Senior engineer");}} this is the end of the introduction of "how to implement the update of java interface composition". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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