Search code examples
selenium-webdriveroverridingabstract-class

Overriding the abstract methods in the subclass


I've two classes, Class Test2 has 2 abstract methods and 1 non-abstract method.

Class Test3 calls Cat() of Test2 and implements two Test2 abstract methods. My question is if I don't add any body to the abstract methods in Test3, have I overridden the methods? I have not implemented the Dog() and Bird(), then why does Test3 compile and run?

Thanks

abstract public class Test2 
{
      abstract public void Dog();
      abstract public void Bird();
       
       public static void Cat()
       {
           System.out.println("Meow");                        
       }
}


public class Test3 extends Test2
{
    @Test
    public void Play()
    {
        Cat();
    }

    @Override
    public void Dog() {
        // TODO Auto-generated method stub
    
    }

    @Override
    public void Bird() {
        // TODO Auto-generated method stub
    
    }
}

Solution

  • Your abstract methods are void and thus return nothing, the compiler expects the method to return nothing and by adding the curly braces you have defined the body; the body is just empty.

    This is similar to when you implement an interface, the only thing the compiler cares about is that you have fulfilled the contract.