Tuesday 25 November 2014

What is abstract ? in OOP Language


You declare a class to be abstract by saying something like:

abstract class Base
{
}

Why would you do this?


To stop it being instantiated.

You cannot then say
Base b = new Base();

But, you can instantiate concrete sub-classes of Base.


Why would you want to stop it being instantiated?

Usually because it is too general.

For example, you might define a class Animal and concrete subclasses of Bird, Cat and Dog.

In the real world you can have Cats and Dogs but not Animals which are not some type of Animal.

Using abstract lets you model this situation.

My First Blog!

My First Blog!