default-method

Why does Java 8 not allow non-public default methods?

对着背影说爱祢 提交于 2019-11-28 21:37:08
问题 Let's take an example: public interface Testerface { default public String example() { return "Hello"; } } public class Tester implements Testerface { @Override public String example() { return Testerface.super.example() + " world!"; } } public class Internet { public static void main(String[] args) { System.out.println(new Tester().example()); } } Simply enough, this would print Hello world! . But say I was doing something else with the return value of Testerface#example , for instance

Java 8 interface default method doesn't seem to declare property

匆匆过客 提交于 2019-11-28 21:36:45
In my application I run into a problem that when a getter in a class is defaulted in an interface only (Java 8 feature), there is no Java Beans property as a result. I.e. for normal method invocation it works just as a standard method, but for access through "properties" it suddenly behaves differently... Here is a test case: import java.beans.Introspector; import java.util.Arrays; import java.util.stream.Collectors; import org.apache.commons.beanutils.PropertyUtils; public class test { public static void main (String[] arguments) throws Exception { // Normal language-level invocation, works

Java 8 add extension/default method to class

六眼飞鱼酱① 提交于 2019-11-28 16:42:36
问题 I am looking for a java equivalent to the C# extension methods feature. Now I have been reading about Java 8's default methods, but as far as I can see, I can only add these to interfaces... ...is there any language feature that will allow me to write an extension method for a final class that doesn't implement an interface? (I'd rather not have to wrap it...) 回答1: C# extension methods are just syntactic sugar for static methods that take the extended type as first argument. Java default

Java 8 default methods vs. non-abstract methods in abstract classes

﹥>﹥吖頭↗ 提交于 2019-11-27 22:12:11
问题 Java 8 interface default methods vs. non-abstract methods in abstract classes - are there any differences between the two (besides the differences of iface - class, visibility etc.) Isn't a default method a step back in Java, meaning it's against the essence that Java has advertised for years?! 回答1: non-abstract methods in abstract classes will be called when it's concrete subclass calls super() if it is overridden. So there are multiple possibilities. If method is not overridden then the

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

六月ゝ 毕业季﹏ 提交于 2019-11-27 15:32:26
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 BImplementor(); carryOutWork(() -> implementsB.doWork()); } } @FunctionalInterface interface

Super class method and Interface default method conflict resolution

隐身守侯 提交于 2019-11-27 14:46:09
Consider the below example, public class Testing extends SupCls implements Intf { public static void main(String[] args) { new Testing().test(); } } class SupCls { public void test() { System.out.println("From SupCls"); } } interface Intf { public default void test() { System.out.println("From Intf"); } } As you can see, there's no connection between SupCls class and Intf interface. But both are defining a common method. And Testing class is extending SupCls and implementing Intf . So, when I call test() method on Testing the output is, From SupCls Which I think makes sense because extending

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

三世轮回 提交于 2019-11-27 09:57:45
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 method2() { // ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here } } Now, one can argue that the two

When is an interface with a default method initialized?

你说的曾经没有我的故事 提交于 2019-11-27 06:02:48
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) throws Exception { InterfaceType foo = new InterfaceTypeImpl(); foo.method(); } } class

Do Java 8 default methods break source compatibility?

元气小坏坏 提交于 2019-11-27 04:09:52
It has generally been the case the Java source code has been forward compatible. Until Java 8, as far as I know, both compiled classes and source have been forward compatible with later JDK/JVM releases. [Update: this is not correct, see comments re 'enum', etc, below.] However, with the addition of default methods in Java 8 this appears to no longer be the case. For example, a library I have been using has an implementation of java.util.List which includes a List<V> sort() . This method returns a copy of the contents of the list sorted. This library, deployed as a jar file dependency, worked

Java 8 default methods as traits : safe?

有些话、适合烂在心里 提交于 2019-11-26 21:18:37
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 { default Logger logger() { return LoggerFactory.getLogger(this.getClass()); } } Or perhaps, define a