84

Here is the MSDN article on abstract classes, but I really don't get it...

When should I really use abstract classes? What are the advantages of using abstract classes?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
bala3569
  • 10,832
  • 28
  • 102
  • 146

6 Answers6

155

Abstract classes are useful when you need a class for the purpose of inheritance and polymorphism, but it makes no sense to instantiate the class itself, only its subclasses. They are commonly used when you want to define a template for a group of subclasses that share some common implementation code, but you also want to guarantee that the objects of the superclass cannot be created.

For instance, let's say you need to create Dog, Cat, Hamster and Fish objects. They possess similar properties like color, size, and number of legs as well as behavior so you create an Animal superclass. However, what color is an Animal? How many legs does an Animal object have? In this case, it doesn't make much sense to instantiate an object of type Animal but rather only its subclasses.

Abstract classes also have the added benefit in polymorphism–allowing you to use the (abstract) superclass's type as a method argument or a return type. If for example you had a PetOwner class with a train() method you can define it as taking in an object of type Animal e.g. train(Animal a) as opposed to creating a method for every subtype of Animal.

Stradivariuz
  • 2,523
  • 1
  • 18
  • 6
  • 15
    Animal would define the need for colour, number of legs, and size properties so when addressing Cat as an Animal then I fervently expect Animal to have a colour, number of legs and size. I also think that you could quite legitimately implement those properties in the base class as to do otherwise would lead to duplicated code. The descriptions here are getting a little caught up in properties rather than methods, there'd be no point in Animal implementing a 'Move' method as each animal may have a different method of locomotion but it would define the need for a 'Move' method. – Lazarus Apr 03 '10 at 17:17
  • The following answer also helped me to understand what an abstract class is https://stackoverflow.com/a/2558588/2188550 –  Jul 11 '18 at 19:28
  • We shouldn't use super class as a return type since it is less specific and this is a bad practice. Return type should be more specific. If Cat is the return type then Animal shouldn't be used as return type. – Ashish Kumar Jaryal Apr 20 '19 at 13:48
6

By using abstract classes we are able to make the class more generic.

For example: if class A is an abstract class and there are classes class B, class C and class D extending abstract class A then these sub-classes will inherit a method which is already declared in abstract class A thereby making the method more generic.

Daniel Hollinrake
  • 1,768
  • 2
  • 19
  • 39
Vivek Saurabh
  • 97
  • 1
  • 1
5

You use them for classes which will never be created (so effectively don't exist), but you want to inherit from them for polymorphism reasons.

Puppy
  • 144,682
  • 38
  • 256
  • 465
5

Richard has provided an example were an abstract class has advantages over non-abstract classes.

I would like to add a fact-table for choosing between an abstract class and an interface. The image can be found here.

enter image description here

Rohit416
  • 3,416
  • 3
  • 24
  • 41
Anton Lyhin
  • 1,925
  • 2
  • 28
  • 34
  • Actually, we can have method bodies anymore in interfaces with c# 8. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods – cansu Dec 07 '21 at 06:13
3

Use abstract classes when you are defining behaviour for a class in your class heirarchy that is never going to be used to instantiate an object directly.

So, think of yourself as God for a moment. Your CBabyBoy and CBanyGirl classes wouldn't be abstract - as these are solid objects that do get created. On the other hand, your CPerson and CAnimal classes WOULD be abstract - they're useful from a type hierarchy point of view, but you won't ever be running CAnimal dingbat = new Animal();

Martin Milan
  • 6,346
  • 2
  • 32
  • 44
2

Basically, you should use an abstract class, when some entity in your hierarchy logically will have method(s) it does not know how to implement, but it's descendants do. There are billions of 'real life' examples all over the web, really)

n535
  • 4,983
  • 4
  • 23
  • 28
  • 3
    Rather than 'does not know' that should really be 'cannot or should not'. Even in the basic Shape object example the draw method could be implemented as an empty method but should it? – Lazarus Apr 03 '10 at 09:56
  • Interesting point of view) But i mean, that empty implementation does not count of course=) – n535 Apr 03 '10 at 10:00