The default method in Java

Source: Internet
Author: User

In "Introduction to 0.3.1 Java", there is a passage: "Please note: Java is not designed as a teaching language." Universities around the world have encountered some pedagogical difficulties in teaching Java (such as the rapid upgrading of the Java language and APIs), which are a general challenge in computer science education. ”

The default method introduced in Java8 fully demonstrates the contradiction between the consistency of the Java platform Concept and the jdk forward compatibility , and satisfies the JDK forward compatibility at the expense of conceptual consistency.

1. Ideals and Realities

The "Once" Java interface is purely a collection of contracts , and is a form of programming expression. From the point of view of data abstraction, it is a powerful and useful mechanism for programming to be able to define type without defining class. The Java interface is a data abstraction composed of these purely interfaces. The Java interface can only have abstract methods, it does not involve any implementations, and it cannot create objects (this is consistent with abstract classes).

The multiple inheritance model leads to additional complexity, the most notable being the diamond problem or "obnoxious diamond derivation" (Dreadful diamond onderivation, DDD). Why the Java interface avoids the complexity of multiple inheritance, the key is that it contains only the abstract method. From a design standpoint, however, the Java interface abandons the intrinsic/intrinsic goals of multiple inheritance and is a stopgap measure.

The interface cannot be upgraded until the "Now" Java8. Because adding a method to an interface causes interrupts for all implementation classes of the old version of the interface. Lambda expression as the core, in order to match the lambda expression, the JDK collection library needs to add new methods, such as foreach (), Stream (), and so on, introduced the default method (defender Methods,virtual Extension methods). It is a library/Framework Design Programmer's regret medicine . For previous legacy code, it is not known that there is a new method, neither called nor implemented, as if it were not present, and programmers who write new code can see it as a guaranteed method body. Any method in the type hierarchy that conforms to the override rule takes precedence over the default method, because the legacy code might just have the same method present.

The default method, in theory, erases the essential difference between the Java interface and the abstract class -the former is a collection of contracts, the latter is the interface and implementation of the binding body . Of course, the difference between the two grammars is the same as before. This requires the programmer to consciously maintain the essential differences between the two, the default method as a library, the framework forward-compatible means .

One of the benefits of the default approach: Multiple inheritance is famous for the diamond problem (the problem) again needs attention. Thus, the assertion that some people previously thought of "introducing an interface mechanism to solve multiple succession problems" became an obvious mistake-a previously mistaken understanding.

For a detailed description of the java1.8 reference http://lucida.me/blog/java-8-lambdas-insideout-language-features/

The default method in Java

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.