Question

I am wondering if there are any differences to derived classes when using abstract vs real classes for inheritance?

It looks to me like the real class inheritance creates an hierarchy whereas abstract class inheritance is just copy-paste code for the compiler to derived classes?

Does the abstract class creates a hierarchy? Can it be accessed using a special keyword?

I know that you use the base keyword to access the base class, but abstract members look just like the original members in the derived classes?

Lastly what's the performance difference between the 2?

Was it helpful?

Solution

Yes the abstract class does exist - the compiler does not do a copy-paste. You will not find any performance difference though as the CLR must still do virtual method calls.

For example the following C#:

abstract class Foo { }

class Bar : Foo { }

generates the following IL:

.class private abstract auto ansi beforefieldinit Foo
    extends [mscorlib]System.Object { }

.class private auto ansi beforefieldinit Bar
    extends Foo { }

The concept of an abstract type is very much a part of the IL.

OTHER TIPS

The only difference is that an abstract base class cannot be instantiated without being derived from, whereas a non-abstract one can. From the point of view of a derived class, everything is the same.

Abstract classes and concrete classes are both tools for (OO) modeling. There is no difference with regard to the inheritance and substitution rules. There are a few technicalities:

  • A abstract class cannot be instantiated
  • Abstract methods are only allowed in abstract classes

But an abstract class can have non-abstract methods, properties and fields.

Performance is not related to the classes but there is a small penalty for abstract/virtual methods.

The power of abstract class is in it unimplementation. It can be used for confidence that instance of derived classes will have the full needed functionality

Abstract class is a primitive form of a base class. These can't be instantiated which means the developer has to derive from these classes to use their functionality. Any abstract method defined in the abstract class must be implemented in its child class. So one can implement methods that must be present in the child class that derives from the abstract class. So you define basic elements of an object that can be customised by the developer in a derived class as he pleases.

Non abstract classes are best used to extend a class functioanlity while also continue to use what is in the non-abstract class. All the non-private members and methods can be accessed by the derived class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top