Search code examples
javaabstract-classextends

abstract class in java-run time polymorphism-superclass reference


I am getting an error while running below code

abstract class A {
    abstract void callMe();

    void callMeToo() {
        System.out.println("this is concrete method");
    }

}

class B extends A {
    void callMe() {
        System.out.println("B's implementation of callme");
    }
}

class AbstractDemo {
    public static void main(String args[]) {

        B b = new B();
        b.callMe();
        b.callMeToo();

    }
}

I got an error message like this below:

run: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: B at abstractdemo.main(abstractdemo.java:28) C:\Users\JARVIS\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:111: The following error occurred while executing this line: C:\Users\JARVIS\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:68: Java returned: 1 BUILD FAILED (total time: 0 seconds)


Solution

  • public class AbstractDemo {
            abstract static class A { 
            public abstract void callMe(); 
            public void callMeToo() { }
          } 
          public static class B extends A { 
              public void callMe() { } 
          }
          public static void main(String args[]) {
    
            B b = new B();
            b.callMe();
            b.callMeToo();
    
         }
    }