I have one common method that I need to use in several classes with only one call to the calling class. So, what I see, I can call it 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]
Uses the default Java 8 feature in the 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 work great for me, but what works best is an abstract class with a non-abstract method OR An interface with a standard method.
source
share