Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract vs Regular Super Class [closed]

Tags:

oop

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?

like image 902
3lgnairt Avatar asked Oct 26 '12 00:10

3lgnairt


People also ask

What is the difference between an abstract class and a 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 ).

Does a super class need to be abstract?

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.

Is an abstract class A superclass Java?

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).

How is an abstract class different from regular classes?

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.


2 Answers

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.)

like image 197
MichaC Avatar answered Oct 26 '22 03:10

MichaC


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.

like image 43
David Titarenco Avatar answered Oct 26 '22 03:10

David Titarenco