Interface improvements for new features of Java 8 (eight villains-1)

Source: Internet
Author: User

Daisy Donergue Daisy

"By woman, are you mean?" She can also call a woman?

Java 8 was released in September 13 and the time to write this blog post is December 17. It's a bit late, but it's necessary to make up the 1.8 feature.

First, the basic introduction

Static methods are already fully defined in the Java 8 interface. A more general example is in the Java class Library, for some interfaces such as Foo, there will be a static method of the tool class Foos to build or match Foo object instances to use. Since static methods can exist in the interface, in most cases the Foos tool class can use the public methods in the interface to proxy (or Foos to package-private).

The above words to understand is that the interface can use the default keyword to modify the interface in the method, breaking the interface cannot write the method of implementation of the shortcomings , but also the original concept has a great impact, because has been said: "Interface can not have the method of implementation", but is not now. So now it's easier to confuse the notion that you can write a method to implement a multiple-inheritance interface, which can be written in a way that is implemented but can only be inherited by a single abstract class. so again asked whether the interface can be written implementation, you can directly reflect the answer to whether you know 1.8. The code is as follows:

Interface Formula {//interface implements the SQRT default algorithm, only need to use the default keyword, in the implementation class only need to implement the Calculate method can use calculate and sqrt two methods    double Calculate (int a);    Default double sqrt (int a) {        return math.sqrt (a);}    }

In the past, adding methods to the interface of the Java class Library was basically impossible. Adding a method to an interface means destroying the code that implements the interface. make up for Java has not supported multiple inheritance, but the interface that supports multiple inheritance cannot write the disadvantage of the method implementation . But now, as long as you can provide an implementation of the correct and sensible default method, the maintainer of the Java class Library can add methods to the interface.

Second, the specific use of the problem

1, the interface multiple inheritance cases, at the same time inherit two or more interfaces have the same method of implementation, then the implementation of the class how do not conflict? When you do not specify which interface the implementation class method points to, the compilation will directly error. so the solution is,

① the method that specifies which parent interface is used in the implementation class,

② overriding methods in implementation classes

2, at the same time inherit the interface and the base class, and the interface and the base class of the same name method are implemented, how to avoid conflicts?

When both the interface and the base class are inherited, when the method conflicts. The implementation class inherits the methods in the base class by default , which means that the base class takes precedence over the interface. Of course, you can also choose to rewrite.

"You John Ruth The Hangman" Hangman Rousse John

"When the hangman catches your, you hang." When the hangman catches you, you'll be hanged.

Mannix ' s Marauders-chris Mannix Marauders Mannix Chris

"I ' m the new sheriff of Red Rock." "I'm the new sheriff of red stone."

Oswaldo Mobray Mobory Osvaldo

"I ' m Oswaldo Mobray, the hangman in these parts." "I am Mobory Osvaldo, the gallows of this area. ”

Joe Gage Getch Chow

"First time in my life I made a pretty penny. And, figured I ' d come home and spend time with my mothr for Christmas. "

"For the first time in my life I made a lot of money, so I wanted to go home and spend Christmas with my mom."

I'm a general.

"You, Sir a hyena." I Hava no wish to speak. "" You're a dirt dog, you don't deserve to talk to me.

Jody Domingre Jody

"How do you doing, dummy?" Are you all right, fool?

Reference Links:

Http://www.oschina.net/translate/everything-about-java-8

Https://www.cnblogs.com/zhouxuanyu/p/4536514.html

Image source: Eight villains (movie)

Interface improvements for new features of Java 8 (eight villains-1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.