default-method

When is an interface with a default method initialized?

北城以北 提交于 2019-11-26 17:34:41
问题 While searching through the Java Language Specification to answer this question, I learned that Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the superinterfaces of an interface are not initialized before the interface is initialized. For my own curiosity, I tried it and, as expected, the interface InterfaceType was not initialized. public class Example { public static void main(String[] args)

Can you call the parent interface's default method from an interface that subclasses that interface? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-26 17:12:28
问题 This question already has an answer here: Explicitly calling a default method in Java 4 answers In java 8 I have something like this: package test; public class SimpleFuncInterfaceTest { public static void carryOutWork(AFunctionalInterface sfi){ sfi.doWork(); } public static void main(String[] args) { carryOutWork(() -> System.out.println("Do work in lambda exp impl...")); AImplementor implementsA = new AImplementor(); //carryOutWork(() -> implementsA.doWork()); BImplementor implementsB = new

What is the reason why “synchronized” is not allowed in Java 8 interface methods?

心已入冬 提交于 2019-11-26 14:58:18
问题 In Java 8, I can easily write: interface Interface1 { default void method1() { synchronized (this) { // Something } } static void method2() { synchronized (Interface1.class) { // Something } } } I will get the full synchronisation semantics that I can use also in classes. I cannot, however, use the synchronized modifier on method declarations: interface Interface2 { default synchronized void method1() { // ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here } static synchronized void

Java8: Why is it forbidden to define a default method for a method from java.lang.Object

久未见 提交于 2019-11-26 14:19:04
Default methods are a nice new tool in our Java toolbox. However, I tried to write an interface that defines a default version of the toString method. Java tells me that this is forbidden, since methods declared in java.lang.Object may not be default ed. Why is this the case? I know that there is the "base class always wins" rule, so by default (pun ;), any default implementation of an Object method would be overwritten by the method from Object anyway. However, I see no reason why there shouldn't be an exception for methods from Object in the spec. Especially for toString it might be very

Purpose of Default or Defender methods in Java 8

ぃ、小莉子 提交于 2019-11-26 11:47:26
Java 8 has included a new feature called Defender methods which allows creation of default method implementation in interface. Now first of all this is a huge paradigm shift for all condensed programmers in Java. I viewed a JavaOne 13 presentation given by Brain Goetz where he was discussing about the new stream() and parallelStream() implementations in Collections library. For adding new methods in Collection interface, they could not have just added a new method without breaking the previous versions. So he told that for catering this a new feature of Default methods was added. public

Java 8 default methods as traits : safe?

亡梦爱人 提交于 2019-11-26 07:55:40
问题 Is it a safe practice to use default methods as a poor\'s man version of traits in Java 8? Some claim it may make pandas sad if you use them just for the sake of it, because it\'s cool, but that\'s not my intention. It is also often reminded that default methods were introduced to support API evolution and backward compatibility, which is true, but this does not make it wrong or twisted to use them as traits per se. I have the following practical use case in mind: public interface Loggable {

Why is “final” not allowed in Java 8 interface methods?

牧云@^-^@ 提交于 2019-11-26 06:09:23
问题 One of the most useful features of Java 8 are the new default methods on interfaces. There are essentially two reasons (there may be others) why they have been introduced: Providing actual default implementations. Example: Iterator.remove() Allowing for JDK API evolution. Example: Iterable.forEach() From an API designer\'s perspective, I would have liked to be able to use other modifiers on interface methods, e.g. final . This would be useful when adding convenience methods, preventing \

Java8: Why is it forbidden to define a default method for a method from java.lang.Object

与世无争的帅哥 提交于 2019-11-26 03:50:47
问题 Default methods are a nice new tool in our Java toolbox. However, I tried to write an interface that defines a default version of the toString method. Java tells me that this is forbidden, since methods declared in java.lang.Object may not be default ed. Why is this the case? I know that there is the \"base class always wins\" rule, so by default (pun ;), any default implementation of an Object method would be overwritten by the method from Object anyway. However, I see no reason why there

Purpose of Default or Defender methods in Java 8

旧城冷巷雨未停 提交于 2019-11-26 02:19:48
问题 Java 8 has included a new feature called Defender methods which allows creation of default method implementation in interface. Now first of all this is a huge paradigm shift for all condensed programmers in Java. I viewed a JavaOne 13 presentation given by Brain Goetz where he was discussing about the new stream() and parallelStream() implementations in Collections library. For adding new methods in Collection interface, they could not have just added a new method without breaking the

Explicitly calling a default method in Java

允我心安 提交于 2019-11-26 00:42:45
问题 Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it\'s possible to explicitly invoke the default implementation of a method when that method has been overridden or is not available because of conflicting default implementations in different interfaces. interface A { default void foo() { System.out.println(\"A.foo\"); } } class B implements A { @Override public void foo() { System.out.println(\"B.foo\