0

I know that abstract classes exist for heritage and we can not take an instance from them. Ok. I wrote a class that not abstract.It's name is A class. And I inherit it some other classes.They are B and C classes.

The question is this: Why I must do A class as abstract? Anyway, A normal class can meet requirements for inheritance. What is the plus of Abstract Classes?

Thanks.

Sinan Türemiş
  • 115
  • 2
  • 6

2 Answers2

1

fist of all, you mentioned one of the advantages, you can't create an instance of that class, and you use the abstract when you explicitly DO NOT want to make an instance of it. for example, your B and C classes share a few fields or methods, now you create an abstract class that contains these methods and fields but at the same time these methods do not compose any logical group, that is why we do not any instance of that specific class and thus we declare it to be abstract.

You may Declare it as a normal class, no complication error in that, but that somehow doe not follow the rules of OOP because then anyone can create an instance of that class which is redundant and unnecessary.

In OOP the point of classes is to gather common features/methods/fields and create them under an object name, an abstract class is not something we'd want to use separately from our B and C classes (for example). Hope that helps.

Safaa Eek
  • 51
  • 2
  • 10
-2

Abstract can be used has thefiloe said to force the reimplementation of class function.

It can also be used has a common interface (Polymorphism) to store the various inheriting class into one single container (list, dict tree ...)

Fawar
  • 735
  • 2
  • 11
  • 32
  • 1
    Can someone explain me why all answer are getting downvoted without any comment. Feels really wrong. – Fawar Jul 13 '14 at 14:31
  • 2
    Probably, because non has any "meat on the bones" and just states the opinions of the authors. So it should be a comment, not an answer. – Uwe Keim Jul 13 '14 at 14:34
  • The question is fairly straight forward. Why use abstract class. Given my answer is the 2 main case why you would. How would you had more meat ? By inventing a use case - writing up fake code about it? That really isnt usefull, he wanted cases about the "why"" here they are? – Fawar Jul 13 '14 at 14:37
  • @Fawar You get those same benefits you mentioned without make the base class abstract. The OP asked specifically about the benefits of making the base class abstract. – itsme86 Jul 13 '14 at 15:09
  • Did he mean purely abstract or abstract as a parent-inheriting class, because that changes the answer - might have understood that wrong – Fawar Jul 13 '14 at 15:11