1

Why do we create abstract classes even though all methods of that class are already defined? If the answer is to stop the programmer from creating an object of that class, couldn't we achieve the same thing by using a private constructor?

Shivam
  • 13
  • 2
  • If you are implementing all the methods, then you need not create an Abstract Class. – TheLostMind Jan 09 '14 at 14:07
  • Can you give us an example of where this is done? – Keppil Jan 09 '14 at 14:09
  • maybe if it's a class containing only static methods, some kind of logic, helper, utility class. – wxyz Jan 09 '14 at 14:09
  • 3
    "we" is a very bad generalization here because I do not see most developers doing this. From my experience, most of the people I work with avoid using abstract classes unless absolutely necessary. – jzworkman Jan 09 '14 at 14:09
  • Most people don't create `abstract` classes unnecessarily. If there is such class it can be a mistake. There is no need for doing that. – Narendra Pathai Jan 09 '14 at 14:11
  • 1
    Please PLEASE do not listen to the people above saying not to or avoid abstract classes. Abstract classes have a place and a purpose... They either never need that purpose, or do not understand its purpose. Abstract classes are almost a staple within Frameworks that are designed to be expanded apon. – WORMSS Jan 09 '14 at 14:16
  • 1
    @WORMSS You are right, but I think the point those other people are making is that abstract classes tend to be over- and misused. (I hope that's what they are saying, at least.) – Marko Topolnik Jan 09 '14 at 14:19

7 Answers7

9

A class being abstract only prevents that particular class from being instantiated. Child classes may still allow instantiation.

A class with no non-private constructors prevents subclassing as well as public instantiation.

From the above you can see that these two things serve two different purposes. A class may have either—or even both properties.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
3

The idea of the Abstract class is a common base for a number of other classes..

Think of "Animals".. You cannot create something called 'Animal'..

You have Cats and Dogs and Rabbits that 'Are' animals.

You have a abstract class called "Animal" and then you have a class called Cat that extends Animal, or Dog that extends Animal... but you do not instantiate the class "Animal" directly as its only a common base.

WORMSS
  • 1,625
  • 28
  • 36
  • This doesn't answer the question. – Paul Bellora Jan 09 '14 at 14:16
  • Read his question again and notice its two questions, and notice I answered the first question which makes his second question relevant. – WORMSS Jan 09 '14 at 14:19
  • I guess you're right - I would just make it clear that `Animal` doesn't necessarily need abstract methods or any methods at all. That way the relevance to the question is clear. – Paul Bellora Jan 09 '14 at 14:23
1

The design pattern of creating a class as abstract even though all methods are defined is used when the abstract class has "do nothing" or exception-throwing implementations of the methods.

We can see this in action in the HttpServlet class, which has implementations for each of the web methods (doGet(), doPost(), doPut() and doDelete()) that throw a ServletException and which a subclass must override if they want a class that does something useful for a particular web method.

Any web methods not overridden with a working implementation will explode by default.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Abstract classes show that this class in itself will not be used independently and some other concrete classes should extend it to make complete sense.

While preventing using private constructor will inhabit subclassing.

Abstract classes with no abstract methods maybe a mistake of the developer.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • Some of the classes of framework are abstract even they contain concrete methods such as ActionForm class of struts framework..... – Shivam Jan 09 '14 at 14:55
0

In Abstract class you can define the constants which are common to many class

upog
  • 4,965
  • 8
  • 42
  • 81
0

If you have a class which only contains static methods, the you can do it abstract, as there's no need of instantiating it. I can think about utility or helper classes at least.

wxyz
  • 709
  • 3
  • 8
  • This wouldn't be any different from using a private constructor, and that is more commonly used for utility classes since it also prevents subtyping. – Paul Bellora Jan 09 '14 at 14:14
0

Specifically regarding the use of abstract vs. hiding the constructor: The abstract keyword more clearly states the architectural intent of the programmer. It's better practice.

The fact that they've provided default implementations of all the methods is a separate question.

keshlam
  • 7,931
  • 2
  • 19
  • 33