0

I'm Currently reading Head First Java, I'm learning about abstract classes and methods, it's all making wonderful sense at the moment but one thing is puzzling me.

The book states that if declaring an abstract method in an abstract class then this method must be overridden by any of the sub-classes, which initially made sense when i learned that abstract methods have no body, my question then is this:

If an abstract method has no body and must also be overridden by any sub-classes, what is the point in declaring an abstract method in the first place, the programmer still has to create the overridden method in each and every sub-class, so why not just forget about the abstract method which has no body anyway, thus alleviating the need to override, it just seems like less code to type or if looking at it the other way more code to type??

Thanks.

D.

N.I.T. E.I.P.
  • 23
  • 1
  • 7
  • So all concrete subclasses of the abstract class are guaranteed to have this method. So that all concrete subclasses can be used interchangeably. – Hovercraft Full Of Eels Jul 23 '17 at 17:00
  • The point is the same as declaring an `interface`: to give code a type (the abstract class in this case) that can be used in place of the concrete type (the concrete sub-class). – markspace Jul 23 '17 at 17:00
  • In the future though, please first [search the site for similar questions](https://www.google.com/search?q=site:stackoverflow.com+java+why+abstract+methods) *before* asking as chances are your question has been asked many times before (here, it's been asked over 50K times before!). – Hovercraft Full Of Eels Jul 23 '17 at 17:01
  • Thanks for responding, while this question may be a duplicate i did however look through the some of the other posts regarding this topic and none of them really satisfied my question which ill admit is more than likely at the moment due to a lack of understanding, but from what i'm gathering at the moment the only real reason i can see in declaring an abstract method (bearing in mind each sub-class would have to write the method anyway by way of overriding) is that if a method is abstract it then forces the subclass to create an overridden method otherwise it wont compile, is that really it? – N.I.T. E.I.P. Jul 23 '17 at 18:37
  • No, that's not all there is to it. Re-read the answers as they in fact do discuss the finer details of this. – Hovercraft Full Of Eels Jul 23 '17 at 18:43

0 Answers0