I have a hard time to choose whether a class should be concrete or abstract. Plus, i don't exactly know at what point in the hierarchy do things become concrete or abstract.Myquestion is, How do we know for sure that a class should be abstract or concrete class?
-
It will be good if you can provide a [minimal example](http://stackoverflow.com/help/mcve), If you want general answer, [See the first result](https://www.google.co.in/search?q=when+to+use+abstract+class&aq=f&oq=when+to+use+abstract+class&aqs=chrome.0.57j0l3j62l2.6617j0&sourceid=chrome&ie=UTF-8#q=when+to+use+abstract+class+in+java). – Not a bug Jul 03 '14 at 06:04
6 Answers
In general, a class should be abstract when you have absolutely no reason to create an instance of that class. For example, suppose you have a Shape class that is the superclass of Triangle, Square, Circle, etc. Since "Shape" is so general, there shouldn't be any reason to construct a Shape object, so it should be abstract (or perhaps even an interface).

- 48
- 5
-
So it is safe to say that an abstract class is a class that cannot be perceived by our senses? Just like the Shape where it only exist as a _concept_ rather than an actual object. – Vincent Mok Jul 04 '14 at 04:27
-
That sounds pretty safe to say. Although, for example, it's possible that you could have an abstract class "Car" with concrete subclasses like "Truck" or "Jeep." – Michael T. Jul 07 '14 at 02:59
Abstract class means it is abstract not complete. It needs another class to complete it and/or its functionality. You need to extend the abstract class.
Few things to keep in mind when you choose abstract class:
- You want that the class should never be instantiated.
- You want the class to be inherited
- The class is not complete and inheriting classes must complete its definition (by overriding abstract methods of the abstract class).

- 7,746
- 2
- 28
- 38
Think of an Abstract class as a normal class, where some of its methods will be defined later.
For example, you want to access the database, but you don't know which database will be used. So you code the part where you open the connection, start the transaction, access the database, close the transaction, and release the connection. But you leave unimplemented the part where the connection is acquired, because that part is database specific. So, someone accessing MySQL, for example, would extend your class and would need to implement only the part where the connection is acquired.

- 8,170
- 10
- 42
- 64
-
Agreed with your example,Would't using a interface for it a better choice? – Abhijeet Panwar Jul 03 '14 at 06:05
-
No, because the interface would not be able to hold the boilerplate code for the example. – Alexandre Santos Jul 03 '14 at 06:09
When there would be a common behavior for all sub classes, a concrete method should be there.
For example- Vehicle is an abstract class, then it is driven by fuel. This will be common behavior of all vehicles (except bicycle) - so that should be described in concrete method, other functions like class (2 wheelers/4 wheelers), running details (mileage 0-20/0-80), power details (100cc-400cc) will be different for different vehicles so it should abstract method.

- 6,801
- 5
- 32
- 55
In my opinion, the only difference between an abstract class and a concrete class is the abstract one cannot be "newed".
You can put an abstract keyword before a concrete class to stop others to new the object of this class. (An abstract class does not have to have abstract method)
I suggest you use interface instead of abstract class, because Java is single-inheritance, a class can just have one super class but can implement multiply interfaces.

- 73
- 1
- 9
An abstract class is good choice :
- If you think you will plan on using inheritance since it provides a common base class implementation to derived classes
- If you want to be able to declare non-public members.
- If you think you will need to add methods in the future
- When you want some methods with definitions – so an abstract class can have non-abstract methods with actual implementation details.
- But remember an abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class.

- 8,806
- 4
- 29
- 34