If everything I can do with an abstract class I can do with a regular super class, why would I ever use an abstract class when I could use a regular super class?
A Number abstract class has an add() method. A OddNumber and an EvenNumber subclass needs to implement virtually the same add() method so there's some code duplication there. Here, having a super class makes more sense (or, at least, having a RealNumber subclass of Number ).
The class should only be abstract if the user needs to implement some logic to make it functional within the framework in which it is being used i.e. where the class designer does not know the exact implementation details of how the class will be used, like the template or command design patterns for example.
An abstract superclass is one way to provide re-usable code. You can extend the abstract class and inherit the code. This is sometimes more convenient than using static methods or object composition to share code. The abstract class can "fix" parts of the code (by making it final).
An abstract class can have abstract methods (Method without body) and concrete/non-abstract methods (Methods with the body) also. The abstract class must have at least one abstract method(Method without body). A normal class can't have any abstract method.
I think the comments and answers have already implied this, but I want to state it more bluntly: You can't do everything with a regular super class that you can do with a abstract class. An abstract class lets you define the signature of a method without including its implementation, and an abstract class won't allow you to instantiate it directly.
So if I have an class signature in mind with 3 methods and I don't want to share the implementations for any of them, I'll use an interface. If I want to share the implementation for one of those methods I'll use an abstract class, then make two of the methods in that class abstract. If I want to share all of the implementations for the methods I'll either use an abstract class, if it never makes sense to instantiate it directly, or I'll use a regular class if it does.
(This is based on my experience in C#. Details differ between languages.)
Base classes provide their own implementation of methods. These implementations can sometimes be overridden (depending on the language).
Abstract classes provide no default implementation and every inheriting class needs to implement the methods on a case-by-case basis.
The above details can vary from language to language. Read the appropriate documentation.
Consider this example:
A Number
abstract class has an add()
method. A OddNumber
and an EvenNumber
subclass needs to implement virtually the same add()
method so there's some code duplication there. Here, having a super class makes more sense (or, at least, having a RealNumber
subclass of Number
).
However, consider Complex
as being a subclass of Number
if number were not abstract, the add()
method found in Number
(assuming it's a real number addition algorithm) would make no sense for complex numbers. Here an abstract class makes more sense. Some languages let you override super-class methods, but that's kind of awkward in this scenario.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With