Why can abstract methods only be declared in abstract classes?
Abstract method basically says, that there is no implementation of the method and it needs to be implemented in a subclass. However if you had an abstract method in a non-abstract class, you could instantiate the class and get an object, that would have an unimplemented method, which you would be unable to call.
Having an abstract method prevents a class from being instantiated, thus making it a de-facto abstract class. Java insists on you declaring this fact explicitly for consistency: technically, Java compiler does not need this additional mark in order to decide if a class is abstract based on the presence of abstract methods, but since you may want to make a class abstract without making any of its methods abstract, requiring the declaration on the class was the way to go.
Lets start by understanding why we need something like a abstract method. The answer is simple. I dont want my extenders to use my methods as it is, I want them to define their own behavior of a particular method. Since I use this method in other methods of my abstract class. I can provide a /**java doc**/ on the abstract class and point them to use a default behavior.
class abstract LoveTheWorld {
private int myKindness ;
public int defaultGiveKindness() {
myKindness -= 5 ;
return 5 ;
}
/**
you can use the defaultGiveKindness method, and not define your own behavior
**/
public abstract int giveKindness() ;
}
This also tells the extender that they can extend only one class (as per java inheritance rules). Now, if you want to twist this story around, you can use interface instead of a abstract class. But it all depends on what constraints do you want your future developer to adhere to, strict or flexible. Strict will keep it tight and ensure reduced mistakes, flexible will keep it loose and free and promote innovation. The question is **what do you need*8.