default-method

Inheritance, composition and default methods

徘徊边缘 提交于 2019-12-04 10:38:15
问题 It is usually admitted that extending implementations of an interface through inheritance is not best practice, and that composition (eg. implementing the interface again from scratch) is more maintenable. This works because the interface contract forces the user to implement all the desired functionality. However in java 8, default methods provide some default behavior which can be "manually" overriden. Consider the following example : I want to design a user database, which must have the

Default interface method for abstract superclass

◇◆丶佛笑我妖孽 提交于 2019-12-04 02:56:39
Lets say I have the following structure: abstract class A { abstract boolean foo(); } interface B { default boolean foo() { return doBlah(); } } class C extends A implements B { //function foo } Java will now complain that class C must implement abstract method foo from A . I can work around this problem relatively easy by redefining the function in C and simply calling B.super.foo(); . however I do not understand why the default function from interface B does not forfill this requirement on its own, and I would like to have a better understanding of the underlying mechanics of java. At your

can marker interface like serializable contain default methods?

帅比萌擦擦* 提交于 2019-12-03 12:44:11
I think it can't, because marker interface principle is to not have any methods, but since default methods are not abstract I am not sure. A "Marker" interface is just a regular interface as far as Java is concerned. Thus, it can have default methods just as any (Java-8) interface can. Now, as to whether this violates the principle of a Marker interface, I would have to say yes. A Marker interface should act as a flag of sorts, only identifying that a class meets some external criteria. Now, it can be a Marker interface and have abstract/default methods, but it will no longer purely meet the

Inheritance, composition and default methods

做~自己de王妃 提交于 2019-12-03 06:34:52
It is usually admitted that extending implementations of an interface through inheritance is not best practice, and that composition (eg. implementing the interface again from scratch) is more maintenable. This works because the interface contract forces the user to implement all the desired functionality. However in java 8, default methods provide some default behavior which can be "manually" overriden. Consider the following example : I want to design a user database, which must have the functionalities of a List. I choose, for efficiency purposes, to back it by an ArrayList. public class

“Property not found on type” when using interface default methods in JSP EL

谁说胖子不能爱 提交于 2019-12-03 05:51:23
问题 Consider the following interface: public interface I { default String getProperty() { return "..."; } } and the implementing class which just re-uses the default implementation: public final class C implements I { // empty } Whenever an instance of C is used in JSP EL scripting context: <jsp:useBean id = "c" class = "com.example.C" scope = "request"/> ${c.property} -- I receive a PropertyNotFoundException : javax.el.PropertyNotFoundException: Property 'property' not found on type com.example

“Property not found on type” when using interface default methods in JSP EL

社会主义新天地 提交于 2019-12-02 19:12:31
Consider the following interface: public interface I { default String getProperty() { return "..."; } } and the implementing class which just re-uses the default implementation: public final class C implements I { // empty } Whenever an instance of C is used in JSP EL scripting context: <jsp:useBean id = "c" class = "com.example.C" scope = "request"/> ${c.property} -- I receive a PropertyNotFoundException : javax.el.PropertyNotFoundException: Property 'property' not found on type com.example.C javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:268) javax.el.BeanELResolver

Significance of inheriting method from superclass instead of default method from implementing interface in java 8

若如初见. 提交于 2019-12-02 01:18:01
I came across following paragraph while reading about java 8 default methods from here : If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from java.lang.Object. The reasoning is very simple, it’s because Object is the base class for all the java classes. So even if we have Object class methods defined as default methods in interfaces, it will be useless because Object class method will always be used. That’s why to avoid confusion, we can’t have default methods that are overriding Object class

Java 8 default interface methods not recognized as managed bean properties in EL

﹥>﹥吖頭↗ 提交于 2019-12-01 02:02:05
问题 I am trying to setup my own JSF tag libary. So I created a composite component with an backing interfaces as a blueprint to build a backing bean for this component. public interface CompLogin { String getUsername(); void setUsername(String username); String getPassword(); void setPassword(String password); String validateLogin(); default String getPasswordWatermark() { return "Passwort"; } default String getUsernameWatermark() { return "Loginname:"; } default String getLoginButtonValue() {

Shadowing default method of an interface

那年仲夏 提交于 2019-11-30 20:33:28
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 which is quite expected. Test Test Test I have been reading about Java-8 default methods and

Java debugger can't call some default method implementations

你说的曾经没有我的故事 提交于 2019-11-30 20:07:35
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"); } } In main() method I put a breakpoint and set up three watches: // Default interface method with