Search code examples
javaabstract-classabstract

Why must a class which contains an abstract method be abstract?


I don't understand why a class, which contains an abstract method, must be abstract.


Solution

  • To look at it another way, say your class with an abstract method did not have to be abstract, and you were allowed to instantiate it, what would the compiler to if you called that method? - there would be no actual code to run. That is why we declare the class abstract - we are marking it as 'not yet ready to use', and later create a 'concrete' subclass with zero abstract methods which can actually be used.

    class Horse { // <== note not actually allowed...
       public abstract int getNeighCount();
    }
    
    Horse horse = new Horse();
    horse.getNeighCount();  // <== what would happen here??