0

I have declared the default method and concrete method in the abstract class with the same name.

public class AbstractClassDefaultMethodTest   {
    public static void main(String[] args) {
        DemoInterface testDefaultMethods=new TestDefaultMethods();
        testDefaultMethods.display();
    }
}
abstract class DemoClass{

    public void display()
    {
        System.out.println("Inside display method of abstract class");
    }
}

interface DemoInterface{

    public default void display()
    {
        System.out.println("Inside display method of interface");
    }
}

class TestDefaultMethods extends DemoClass implements DemoInterface
{

}

Why the concrete method of abstract class is getting called? Why we are not getting the compile time error, as both the method names are same ?

implosivesilence
  • 536
  • 10
  • 24
  • 1
    Why do you expect a compiler error? The method in `DemoClass` simply *overrides* the method of the same name in `DemoInterface`. And you know how overridden methods work. – ernest_k May 20 '21 at 13:39
  • 1
    default methods have only been made possible since java 8. They had to be able to implement them, without breaking existing code, so that means adding a default method shouldn't be allowed to break code or change functionality. – Stultuske May 20 '21 at 13:41

0 Answers0