10

I was doing a kind of R&D and am confused with the concept of an abstract class.

What I know about an abstract class is that it may contain concrete methods, and it may contain virtual methods. It may or may not contain an abstract method, and it may contain fields and restricting the direct creation of instances.

But we can achieve all these in a simple base class (an addition of virtual methods will do a lot because a base class with virtual methods which doesn't contain an implementation there and override is same as an abstract method). Then why do we need an abstract class though interface support multiple inheritance and events?

Satinder singh
  • 10,100
  • 16
  • 60
  • 102
peter
  • 8,158
  • 21
  • 66
  • 119
  • StackOverflow tags 'abstract-class' and 'base-class' in question also have good definitions for reference to differentiate between abstract & base class even if abstract class does not have any abstract methods................ Abstract classes are classes which cannot be instantiated. They exist to provide common functionality and interface specifications to several concrete classes.................In Object Oriented Programming, a base class is one from which other classes inherit. For example, a child-class `Male` and another child-class `Female` may both inherit from the base-class `Human`. – firstpostcommenter Aug 07 '19 at 12:56

4 Answers4

22

But we can achieve all these in a simple baseclass

No, you can't. You can't have a non-abstract class that has abstract methods (methods where the signature is defined, but no implementation is given, thus forcing derived classes to provide an implementation).

Abstract classes exist so that they can provide a combination of implemented methods and abstract methods.

You can attempt to avoid using abstract classes by using concrete implementations that just don't do anything, but it has a number of drawbacks:

  • It doesn't force the caller to override the implementation.
  • It gives the impression that the type and those methods in particular are working, when in fact they are not. By adding the feature of abstract methods and types you prevent the unintended use of an incomplete type by someone who doesn't realize that it's incomplete.
  • It provides a clear contract to sub-classes as to what functionality they need to provide, versus what methods of the base class are working but can optionally be extended.

Also consider non-void methods in a world without abstract. They would need to throw (i.e. NotImplementedException) or return an invalid value (i.e. null). This could be quite a lot worse than a method that just does nothing.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • what would be then a base class with virtual methods?what is the difference , forget about name abstract methods.Because virtual methods also we can put it as abstract methods and override it – peter Sep 19 '13 at 17:25
  • @peter You can't forget about them. They're the single key difference. *If* you forget them, it leaves you with nothing, but you *can't* forget them, because they are the *point* of an abstract class. If you *don't* have any abstract methods then you don't need an abstract class. – Servy Sep 19 '13 at 17:27
  • what i was intended is virtual methods also we can put it as abstract methods which doesnot have any implementation and override it – peter Sep 19 '13 at 17:28
  • 1
    @peter That doesn't force the caller to override the implementation, and gives the impression that the type and those methods in particular are working, when in fact they are not. By adding the feature of abstract methods and types you prevent the unintended use of an incomplete type by someone who doesn't realize that it's incomplete. It also provides a clear contract to sub-classes as to what functionality they need to provide. In particular consider non-void methods in which the base implementation in a world without `abstract` would need to throw or return an invalid value. – Servy Sep 19 '13 at 17:30
  • that is excellent comment, can you edit your answer with this comment – peter Sep 19 '13 at 17:34
19

The main difference is the compiler won't let you instantiate an abstract class, while you could instantiate a base class (which may not make sense).

AbstractType a = new AbstractType(); //compiler error
AbstractType d = new DerivedType(); //OK

BaseType b = new BaseType(); //OK

Notice with the variable d we are guaranteed that the abstract methods have been overridden (otherwise the DerivedType class would have a compiler error).

Since you are commenting a lot of confusion still I'll give you the example I had that really made this concept click for me. Imagine you are making a tower defense game. You have a tower class, and every tower has the ability to attack, so you make an abstract tower class like this:

abstract class Tower
{
    abstract void Attack();
}

Now I can make several tower classes:

class FlameTower : Tower
{
    override void Attack()
    {
        //Shoot flames at everyone
    }
}

class IceTower : Tower
{
    override void Attack()
    {
        //Shoot ice everywhere
    }
}

Now if you want to declare a list of towers, you can write:

 List<Tower> towerList = new List<Tower>();
 towerList.Add(new FireTower());
 towerList.Add(new IceTower());

then iterate through them and make them all attack:

 foreach (Tower t in towerList)
 {
     t.Attack();
 }

And every class is guaranteed to have implemented attack because it was marked abstract and would be a compile error if they did not. Now all this could be done with a base class, except a base class would allow this:

 towerList.Add(new Tower());

Now when it tries to call attack on that new Tower() it's going to hit a blank abstract method, which is not what we want. So to forbid declaring something as a generic tower, we make the class abstract, then we know everything will have it's own definition of Attack and it will do something.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • what is the advantage here? we can do indirectly like abstractclass ab= new derviced class(); – peter Sep 19 '13 at 17:18
  • @peter but then you have a derived class instead of an abstract class (so you are ensured the abstract methods have a definition) – Kevin DiTraglia Sep 19 '13 at 17:18
  • Great code example. It's similar to what I discussed with animals above. For example, if `abstract class Animal` has function `public abstract void eatStuff()`, it's guaranteed that every concrete instance of `Animal` knows how to do its own eating. – musical_coder Sep 19 '13 at 17:49
  • 3
    @musical_coder Yeah I always hated the animal example because it's hard for me to visualize writing code for an animal, I like to give examples related to making games, because everybody loves making games! – Kevin DiTraglia Sep 19 '13 at 17:51
3

One simple answer could be:-

Base classes have their own implementations for methods, and these implementations can be used/added to in an inherited class. You can instantiate a base class

Abstract classes have declarations for class methods. Any class inheriting the abstract class must implement it's abstract methods or becomes to abstract itself. You can not instantiate a abstract class

Example:-

public abstract class A
{
   public void Method1()
   {
     //method code
   }

   public abstract void Method2();

   public virtual void Method3()
   {
    //method code for class A, when class A calls Method3, this code is executed
   }
}

public class B : A
{
   public override void Method2()
   {
     //this must be implemented here to use it
   }

   public override void Method3()
   {
     //method code for class B, when class B calls Method3, this code is executed
     //or, if you choose not to override this method, the compiler will use the code in class A
   }
}

class B can still use Method1 from class A because it's inherited. And if class A is to be abstract, then all of the methods would be declared like Method2 and would have to be implemented in class B so that it can be used.

JSON
  • 1,113
  • 10
  • 24
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
3

There are some classes which just don't exist in the real world, and so should conceptually be marked as abstract. For example, consider abstract class Animal. There's no such thing as an "animal" out in the real world. Instead, there are concrete types of animals such as Dog, Cat, etc.

musical_coder
  • 3,886
  • 3
  • 15
  • 18
  • then this can be possible with base class contains virtual methods, i have done a bit modification of my question – peter Sep 19 '13 at 17:24