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 use the new features of interfaces in Java8

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

Share

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

This article mainly introduces "how to use the new features of the interface in Java8". In the daily operation, I believe that many people have doubts about how to use the new features of the interface in Java8. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the new features of the interface in Java8". Next, please follow the editor to study!

In Java8, you can add static and default methods to an interface.

Static method: decorate with the static keyword. You can call a static method directly through the interface and execute its method body

The default method: decorate with the default keyword. Can be called through a class

Just look at the code.

New feature in package com.nanfeng.demo.interfacepractice.java8; / * java8 * the permission of the default method in the interface is public, so public can also omit * / public interface CompareA {/ / the abstract method public static void method1 () {System.out.println can be defined in the interface ("static methods can be defined in the interface in Java8, and-1 can be called through the interface") } / / default method public default void method2 () {System.out.println ("default method can be defined in interfaces in Java8-- 2");} public default void method3 () {System.out.println ("interfaces in Java8 can define default methods-- 3");}} write test class ComepareTestpackage com.nanfeng.demo.interfacepractice.java8 Public class CompareATest {public static void main (String [] args) {/ / create an implementation class object CompareAClass c = new CompareAClass () / * knowledge No.1: static methods in an interface can only be called through an interface * Static method may be invoked on containing interface class only * static methods can only be called * / c.method1 () when an interface class is included / / call the method1 () method using the interface, which is a bit like a tool class called CompareA.method1 () / * * knowledge No.2: * default method. You can call the default method in the interface by creating an object that implements the class * or you can override the default method in the interface * / c.method2 () }} class CompareAClass implements CompareA {/ * when overriding the default method in the interface in the implementation class * Note: public permission modification cannot be omitted, otherwise an error will be reported * our rewritten method will still be called when it is executed Conforms to inheritance * / @ Override public void method2 () {System.out.println ("the implemented class in Java8 can override the default method in the interface, note that the permission modifier of the declared method is public, and public cannot be omitted") }}

Running result:

Static methods can be defined in the interface in Java8, which can be called through the interface.

The implementation class in Java8 can override the default method in the interface. Note that the permission modifier of the declared method is public, and public cannot be omitted-- 2

In the first case, a class implements the interface while inheriting a parent class

1. Create the parent class of the implementation class

The parent class of the package com.nanfeng.demo.interfacepractice.java8; / * implementation class * / public class SuperClass {/ / defines a method with the same name as the method public void method3 () {System.out.println ("in the parent class of the implementation class, a method with the same name and parameter appears in the interface-SuperClass");}}

2. Let the subclass inherit the parent class while implementing the interface

Package com.nanfeng.demo.interfacepractice.java8; public class CompareATest {public static void main (String [] args) {/ / create an implementation class object CompareAClass c = new CompareAClass () / * knowledge No.1: static methods in an interface can only be called through an interface * Static method may be invoked on containing interface class only * static methods can only be called * / c.method1 () when an interface class is included / / call the method1 () method using the interface, which is a bit like a tool class called CompareA.method1 () / * * knowledge No.2: * default method. You can call the default method in the interface by creating an object that implements the class * or you can override the default method in the interface * / c.method2 () / * knowledge point 3: * what should I do if the method in the parent class has the same name as the method in the interface? * if a method with the same name and parameter is declared in the parent class inherited by the subclass (or implementation class) and in the interface of the implementation * then when the subclass does not override this method, the default is to call the method with the same name and parameter in the parent class. *-- > Class priority principle * / c.method3 () }} class ComepareAClass extends SuperClass implements CompareA {/ * when overriding the default method in the interface in the implementation class * Note: public permission modification cannot be omitted, otherwise an error will be reported * our rewritten method will still be called when it is executed Conforms to inheritance * / @ Override public void method2 () {System.out.println ("the implemented class in Java8 can override the default method in the interface, note that the permission modifier of the declared method is public, and public cannot be omitted") }}

Run to view the results:

Static methods can be defined in interfaces in Java8, which can be called through interfaces-- 1.

The implementation class in Java8 can override the default method in the interface. Note that the permission modifier of the declared method is public, and public cannot be omitted-- 2

In the parent class of the implementation class, a method with the same name as the interface-SuperClass appears

Case two

How should the implementation class implement multiple interfaces without inheriting the parent class?

1. Create the CompareB API and create the default method with the same parameters as the CompareA API.

Note: if a class inherits multiple interfaces at the same time, an interface conflict occurs when a default method with the same name and parameter appears in the interface. At this point, the implementation class must override this method

/ * multiple inheritance of interfaces in the Java interface * case 1: when a method with the same name and parameter occurs in the class ComepareAClass extends SuperClass implements CompareA * CompareA interface and the SuperClass parent class, the method in the parent class is called by default, which reflects the principle of class priority * case 2: * class ComepareAClass implements CompareA, CompareB * under the premise that a class inherits from multiple interfaces without inheriting the parent class The default method with the same name and parameter is defined in the two interfaces, how will it be implemented? * knowledge point 4: * if an implementation class implements multiple interfaces and a default method with the same name and parameter is defined in the multiple interfaces, then if the implementation class does not override this method Error report *-- > there will be an excuse conflict * this requires us to rewrite this method in the implementation class * * / class CompareAClass implements CompareA, CompareB {/ * when overwriting the default method in the interface in the implementation class * Note: do not omit the public permission modification, otherwise it will report an error * when executing, our rewritten method will still be called Conforms to inheritance * / @ Override public void method2 () {System.out.println ("the implementation class in Java8 can override the default method in the interface, note that the permission modifier of the declared method is public, and public cannot be omitted-- 2") } / / to resolve interface conflicts, the method in the interface must be rewritten @ Override public void method3 () {System.out.println ("the ComepareAClass implementation class overrides the method of the same name in multiple interfaces, and after the rewrite executes the method in the implementation class-- method3 ()");}}

Running result:

Static methods can be defined in interfaces in Java8, which can be called through interfaces-- 1.

The implementation class in Java8 can override the default method in the interface. Note that the permission modifier of the declared method is public, and public cannot be omitted-- 2

The ComepareAClass implementation class overrides the method of the same name in multiple interfaces, and executes the method in the implementation class-- method3 ().

Situation three

In methods defined by themselves in a subclass (or implementation class), methods that are not overridden in the interface and parent class are called

/ * multiple inheritance of interfaces is supported in the Java interface * case 1: when a method with the same name and parameter appears in the class ComepareAClass extends SuperClass implements CompareA * CompareA interface and the SuperClass parent class, the method in the parent class is called by default, which reflects the principle of class priority * case 2: * class ComepareAClass implements CompareA, CompareB * under the premise that a class inherits from multiple interfaces without inheriting the parent class The default method with the same name and parameter is defined in the two interfaces, how will it be implemented? * knowledge point 4: * if an implementation class implements multiple interfaces and a default method with the same name and parameter is defined in the multiple interfaces, then if the implementation class does not override this method Reporting an error *-- > there will be an excuse conflict * this requires us to rewrite this method in the implementation class * case 3: * class CompareAClass extends SuperClass implements CompareA, CompareB * A subclass (or implementation class) inherits the parent class while implementing multiple interfaces * in the methods defined by the subclass (or implementation class) Call the method * * / class CompareAClass extends SuperClass implements CompareA that is not overridden in the interface and parent class. When CompareB {/ * overrides the default method in the interface in the implementation class, * Note: do not omit the public permission modification, otherwise an error will be reported * when executing, our rewritten method will still be called. Conforms to inheritance * / @ Override public void method2 () {System.out.println ("the implementation class in Java8 can override the default method in the interface, note that the permission modifier of the declared method is public, and public cannot be omitted-- 2") } / / to resolve the interface conflict, the method in the interface must be rewritten @ Override public void method3 () {System.out.println ("the ComepareAClass implementation class overrides the method with the same name in multiple interfaces, and executes the method in the implementation class-- method3 ()") Knowledge point 5: how to call a method that is not overridden in the parent class (or interface) in the method of the subclass (or implementation class) * / public void myMethod () {/ / call the self-rewritten method3 () method this.method3 (); / / call the method3 () method super.method3 () declared in the parent class / / call the default method in the interface (Note: it is a non-static method, so you cannot use the interface name to call it) / / call method: interface name. Super. Method CompareA.super.method3 (); CompareB.super.method3 ();}} at this point, the study on "how to use the new features of interfaces in Java8" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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