I have one common method which i need to use in several classes with just a single call from the calling class. So what i see is i can call it in two ways.
public abstract class TestAbstractClass {
void commonMethod(){
System.out.println("Calling common method : TestAbstractClass");
}
}
calling class:
public class RunApplication extends TestAbstractClass{
public void testMethod(){
commonMethod();
}
}
[OR]
Using Java 8 feature of default method in interface.
public interface TestInterface {
default void commonMethod(){
System.out.println("Calling common method : TestInterface");
}
}
calling class:
public class RunApplication implements TestInterface{
public void testMethod(){
commonMethod();
}
}
They both works fine for me, But what is better approach, an abstract class with non-abstract method OR Interface with default method.