Explicitly calling a default method in Java

As per this article you access default method in interface A using A.super.foo(); This could be used as follows (assuming interfaces A and C both have default methods foo()) public class ChildClass implements A, C { @Override public void foo() { //you could completely override the default implementations doSomethingElse(); //or manage conflicts between the same … Read more

Java Pass Method as Parameter

Edit: as of Java 8, lambda expressions are a nice solution as other answers have pointed out. The answer below was written for Java 7 and earlier… Take a look at the command pattern. // NOTE: code not tested, but I believe this is valid java… public class CommandExample { public interface Command { public … Read more

Traits vs. interfaces

Public Service Announcement: I want to state for the record that I believe traits are almost always a code smell and should be avoided in favor of composition. It’s my opinion that single inheritance is frequently abused to the point of being an anti-pattern and multiple inheritance only compounds this problem. You’ll be much better … Read more

Why are C# 4 optional parameters defined on interface not enforced on implementing class?

UPDATE: This question was the subject of my blog on May 12th 2011. Thanks for the great question! Suppose you have an interface as you describe, and a hundred classes that implement it. Then you decide to make one of the parameters of one of the interface’s methods optional. Are you suggesting that the right … Read more

Should we @Override an interface’s method implementation?

You should use @Override whenever possible. It prevents simple mistakes from being made. Example: class C { @Override public boolean equals(SomeClass obj){ // code … } } This doesn’t compile because it doesn’t properly override public boolean equals(Object obj). The same will go for methods that implement an interface (1.6 and above only) or override … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)