Saturday, June 25, 2011

Why object of abstract class can’t be instantiate

We all knows that object of abstract class can be created but cannot be instantiated, but important question is why its not allowed to instantiate the object of abstract class?

Lets understand it with an example:
 public abstract  class cal
    {
        public cal()
        {

        }
        public cal(int a)
        {

        }
        public abstract int add(int a, int b);

    }

public class c2 
    {
            cal c;// Yes we can create object of abstract class
            c = new cal() // error, we can't instantiate object of abstract class.

       
    }

as we can see in above example that we cannot instantiate the object of an abstract class, this is because if it allows to instantiate the object of abstract class and if you will call its function named "add" (abstract) then what will happen? as there is no function body/functionality is defined for that "add" method, as its functionality will be defined in its child class so CLI not allows us to instantiate the object of abstract class.

An abstract class has a protected constructor (by default) allowing derived types to initialize it.

An abstract type is defined largely as one that can't be created. You can create subtypes of it, but not of that type itself. The CLI will not let you do this.

Share This!


3 comments:

Anonymous said...

Nice !!!

Khumesh Kumawat said...

Very-2 Nice Post!!

Anonymous said...

Good info.. Keep going

Powered By Blogger · Designed By Seo Blogger Templates