Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract Base Class vs. Concrete Class as a SuperType

After reading the most excellent book "Head First Design Patterns", I began proselytizing to my colleagues the benefits of patterns and design principles. While extolling the virtues of my favorite pattern - Strategy Pattern - I was asked a question which gave me pause. Strategy, of course, uses inheritance and composition and I was on one of my tirades about "program to an interface (or supertype) not an implementation", when a colleague asked "why use an abstract base class instead of a concrete class?".
I could only come up with "well you force your subclasses to implement abstract methods and prevent them from instantiating the ABC". But to be honest the question caught me off gaurd. Are these the only benefits of using an abstract base class over a concrete class at the top of my hierarchy?

like image 226
Tony D Avatar asked Jun 28 '10 17:06

Tony D


People also ask

Can abstract class have abstract as well as concrete methods?

Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract. Abstract class can not be instantiated using new keyword.

What is the difference between an abstract class and a concrete class?

An abstract class is a class declared with an abstract keyword which is a collection of abstract and non-abstract methods while a concrete class is a class that allows creating an instance or an object using the new keyword. Thus, this is the main difference between abstract class and concrete class.

Can you explain the difference between an interface abstract class and a concrete class?

Interfaces are a kind of code contract, which must be implemented by a concrete class. Abstract classes are similar to normal classes, with the difference that they can include abstract methods, which are methods without a body. Abstract classes cannot be instantiated.

What is the benefit of an abstract base class?

The abstract class in Java enables the best way to execute the process of data abstraction by providing the developers with the option of hiding the code implementation. It also presents the end-user with a template that explains the methods involved.


2 Answers

If you need specific methods to be implemented, then use an Interface. If there is shared logic that can be pulled out, use an abstract base class. If the base set of functionality is complete on its own, then you can use a concreate class as the base. An abstract base class, and an Interface cannot be instantiated directly, and that is one of the advantages. If you can use a concrete type, then you need to do override methods, and that has a "code smell" to it.

like image 102
NerdFury Avatar answered Oct 20 '22 07:10

NerdFury


Program to interface, not to implementation has little to do with abstract and concrete classes. Remember the template method pattern? Classes, abstract or concrete, are the implementation details.

And the reason to use abstract classes instead of concrete classes is that you can invoke methods without implementing them, but by leaving them to be implemented to subclasses instead.

Programming to an interface is a different thing - it is defining what your API does, not how it does it. And this is denoted by interfaces.

Note one key difference - you can have protected abstract methods, which means that this is implementation detail. But all interface methods are public - part of the API.

like image 36
Bozho Avatar answered Oct 20 '22 08:10

Bozho