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 understand the default and static methods in the Java8 interface

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

Share

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

This article introduces the knowledge of "how to understand the default and static methods in the Java8 interface". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Default and static methods in the interface

Java 8 allows the interface to include a method with a specific implementation, called the "default method", which is decorated with the default keyword.

The class first principle of the default method of the interface. If a default method is defined in one interface and a method with the same name is defined in another parent class or interface

Select a method in the parent class. If a parent class provides a specific implementation, the default method with the same name and parameters in the interface is ignored.

Interface conflict. If one parent interface provides a default method and another interface provides a method with the same name and parameter list (regardless of whether the method is the default method or not), the method must be overridden to resolve the conflict.

At the same time, Java8 allows static methods (static) to be added to the interface.

two。 Application examples package com.szh.java8; / * * * / interface InterfaceOne {default String getMessage () {return "ha ha";} static void show () {System.out.println ("static method in interface");}} interface InterfaceTwo {default String getMessage () {return "hehe";}} class ClassOne {public String getMessage () {return "hehe" } class SubClass1 extends ClassOne implements InterfaceOne {} class SubClass2 extends ClassOne implements InterfaceOne,InterfaceTwo {} class SubClass3 implements InterfaceOne,InterfaceTwo {@ Override public String getMessage () {return InterfaceTwo.super.getMessage ();}} public class TestClass {public static void main (String [] args) {SubClass1 sub1 = new SubClass1 (); System.out.println (sub1.getMessage ()); SubClass2 sub2 = new SubClass2 () System.out.println (sub2.getMessage ()); SubClass3 sub3 = new SubClass3 (); System.out.println (sub3.getMessage ()); InterfaceOne.show ();}}

Although the SubClass1 class inherits ClassOne and implements InterfaceOne, the getMessage method in the ClassOne class is executed here because of the class priority principle.

SubClass2 is the same as SubClass1.

SubClass3 is different, it does not inherit the ClassOne class, but implements both InterfaceOne and InterfaceTwo interfaces, so it must choose one of them to implement the default method (override / rewrite) and execute the default method of which interface is implemented accordingly.

Finally, the static method in the interface is the same as the static method in the class, the direct interface name. The static method name can be called.

That's all for "how to understand the default and static methods in the Java8 interface". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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