Abstract versus regular superclass

If all I can do with an abstract class I can do with a regular superclass, why would I use an abstract class when I could use a regular superclass?

+4
source share
3 answers

I think the comments and answers already implied this, but I want to say it more directly: you cannot do everything with the usual superclass, which you can do with an abstract class. An abstract class allows you to determine the signature of a method without including its implementation, and an abstract class does not allow you to create it directly.

So, if I have a subclass of a class with three methods, and I donโ€™t want to share implementations for any of them, I will use the interface. If I want to share an implementation for one of these methods, I will use an abstract class, and then make two methods in this abstract class. If I want to share all the implementations of the methods, I will either use an abstract class if it does not make sense to create it directly, or I will use a regular class if so.

(This is based on my experience in C #. Details differ between languages.)

+10
source

Base classes provide their own implementation of methods. These implementations can sometimes be overridden (depending on the language).

Abstract classes do not provide a default implementation, and each inheriting class must implement methods in each case.

The above details may vary from language to language. Read the relevant documentation.

Consider this example:
A Number abstract class has a method add() . A subclass of OddNumber and EvenNumber should implement almost the same add() method so that there is some duplication of code. Here the superclass makes sense (or at least has a subclass of RealNumber Number ).

However, consider Complex as a subclass of Number , if the number was not abstract, the add() method found in Number (provided that it is an algorithm for adding extra numbers) does not make sense for complex numbers. Here the abstract meaning makes sense. Some languages โ€‹โ€‹allow you to override superclass methods, but in this case it's awkward.

+1
source

Abstract classes are very useful when you need to represent many similar things in the usual way.

For example, say you wanted to represent the zoo in code, and you had to get a list of products to make sure you have the right food. How do you imagine your favorite food for all your animals? And what noise does everyone make? How about how they move? You can try to use a superclass to store large lists of products, noises and movements, but itโ€™s easier to have a common definition for an Animal, and then each animal provides its own implementation details:

  public class Zoo { public Animals[] AnimalsInZoo { get; set;} public List<Food> GetGroceryList(){ List<Food> groceries = new List<Food>(); foreach(Animal a in Animals[]){ groceries.Add(a.FavoriteFood); } return groceries; } public void MakeAnimalsSing(){ foreach(Animal a in Animals[]){ a.MakeNoise(); } } } 

An abstract class of animals will look like this:

  public abstract class Animal { public abstract void MakeNoise(); public abstract Food FavoriteFood { get; } public abstract void Move(); } 

And let's say there were two kinds of animals in the zoo:

  public class Panda : Animal{ public override void MakeNoise(){ // grunt } public override void Move(){ // walk } public override Food FavoriteFood { get { return new Bamboo(); } } } public class Parrot: Animal{ public override void MakeNoise(){ // talk } public override void Move(){ // fly } public override Food FavoriteFood { get { return new Cracker(); } } } 

Having a common abstract class, I can easily work with any number of different types of animals, not knowing who I work with, but everyone can behave differently.

A superclass simply cannot provide flexibility or detail.

0
source

Source: https://habr.com/ru/post/1442217/


All Articles