Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can abstract methods only be declared in abstract classes?

Tags:

I understand that in abstract classes methods be both abstract, or not. But why can I not create an abstract method in a "normal", non-abstract class?

Thanks in advance for any explanation!

like image 809
Jona Avatar asked Mar 04 '14 01:03

Jona


2 Answers

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.

like image 109
Warlord Avatar answered Oct 19 '22 04:10

Warlord


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.

like image 27
Sergey Kalinichenko Avatar answered Oct 19 '22 05:10

Sergey Kalinichenko