default-method

Why no default clone() in Cloneable in Java 8

微笑、不失礼 提交于 2019-11-30 13:56:15
问题 Cloneable in Java is inherently broken. Specifically, my biggest problem with the interface is it expects a method behavior that doesn't define the method itself. So if traversing through a Cloneable list you must use reflection to access its defined behavior. However, in Java 8, we now have default methods and now I ask why there isn't a default clone() method in Cloneable . I understand why interfaces cannot default Object methods, however, this was an explicit design decision and so

Calling default method in interface when having conflict with private method

一世执手 提交于 2019-11-30 10:44:52
Consider below class hierarchy. class ClassA { private void hello() { System.out.println("Hello from A"); } } interface Myinterface { default void hello() { System.out.println("Hello from Interface"); } } class ClassB extends ClassA implements Myinterface { } public class Test { public static void main(String[] args) { ClassB b = new ClassB(); b.hello(); } } Running the program will give following error : Exception in thread "main" java.lang.IllegalAccessError: tried to access method com.testing.ClassA.hello()V from class com.testing.Test at com.testing.Test.main(Test.java:23) This is all

Why no default clone() in Cloneable in Java 8

荒凉一梦 提交于 2019-11-30 08:55:59
Cloneable in Java is inherently broken. Specifically, my biggest problem with the interface is it expects a method behavior that doesn't define the method itself. So if traversing through a Cloneable list you must use reflection to access its defined behavior. However, in Java 8, we now have default methods and now I ask why there isn't a default clone() method in Cloneable . I understand why interfaces cannot default Object methods , however, this was an explicit design decision and so exceptions can be made. I sort of envision deprecating Object.clone() and changing its interior code to

Shadowing default method of an interface

元气小坏坏 提交于 2019-11-30 05:10:22
问题 Consider the following case, interface IFace1 { default void printHello() { System.out.println("IFace1"); } } interface IFace2 { void printHello(); } public class Test implements IFace1, IFace2 { public static void main(String[] args) { Test test = new Test(); test.printHello(); IFace1 iface1 = new Test(); iface1.printHello(); IFace2 iface2 = new Test(); iface2.printHello(); } @Override public void printHello() { System.out.println("Test"); } } In above example I am getting following output

Java debugger can't call some default method implementations

梦想与她 提交于 2019-11-30 04:05:14
问题 I'm coding in IntelliJ IDEA. When debugging my application, I can't use some default method implementations in Watches . Here is a condensed example: public class Friendship { interface Friend { default void sayHiTo(Friend friend) { System.out.println("Hi, " + friend.hashCode()); } default int amountOfHands() { return 2; } } public static class BasicFriend implements Friend { int numberOfFaces() { return 1; } } public static void main(String[] args) { System.out.println("Put a breakpoint here

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

心已入冬 提交于 2019-11-29 23:52:40
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 initializing a data file and returning a sensitive internal value that shouldn't leave the implementing

Calling default method in interface when having conflict with private method

拜拜、爱过 提交于 2019-11-29 15:55:14
问题 Consider below class hierarchy. class ClassA { private void hello() { System.out.println("Hello from A"); } } interface Myinterface { default void hello() { System.out.println("Hello from Interface"); } } class ClassB extends ClassA implements Myinterface { } public class Test { public static void main(String[] args) { ClassB b = new ClassB(); b.hello(); } } Running the program will give following error : Exception in thread "main" java.lang.IllegalAccessError: tried to access method com

Java default interface methods concrete use cases

空扰寡人 提交于 2019-11-29 08:46:33
问题 Java 9 is near to come and more features will be added to Java interfaces, like private methods. default methods in interfaces were added in Java 8, essentially to support the use of lambdas inside collections without breaking retro-compatibility with previous versions of the language. In Scala, methods inside trait s are quite useful. However, Scala has a different approach treating trait s than Java with default methods. Think to multiple inheritance resolution or the use of trait s as

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

半世苍凉 提交于 2019-11-29 03:45:56
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?! 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 super class method will be executed. if we use super() in the concrete subclass method then the overridden

How to explicitly invoke default method from a dynamic Proxy?

爷,独闯天下 提交于 2019-11-29 02:56:25
Since Java 8 interfaces could have default methods. I know how to invoke the method explicitly from the implementing method, i.e. (see Explicitly calling a default method in Java ) But how do I explicitly invoke the default method using reflection for example on a proxy? Example: interface ExampleMixin { String getText(); default void printInfo(){ System.out.println(getText()); } } class Example { public static void main(String... args) throws Exception { Object target = new Object(); Map<String, BiFunction<Object, Object[], Object>> behavior = new HashMap<>(); ExampleMixin dynamic =