Why can't an object of abstract class be created?

96,961

Solution 1

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.

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

For example, the base-type Stream is abstract. Without a derived type where would the data go? What would happen when you call an abstract method? There would be no actual implementation of the method to invoke.

Solution 2

Because it's abstract and an object is concrete. An abstract class is sort of like a template, or an empty/partially empty structure, you have to extend it and build on it before you can use it.

Take for example an "Animal" abstract class. There's no such thing as a "pure" animal - there are specific types of animals. So you can instantiate Dog and Cat and Turtle, but you shouldn't be able to instantiate plain Animal - that's just a basic template. And there's certain functionality that all animals share, such as "makeSound()", but that can't be defined on the base Animal level. So if you could create an Animal object and you would call makeSound(), how would the object know which sound to make?

Solution 3

It's intended to be used as a base class.

http://msdn.microsoft.com/en-us/library/sf985hc5(VS.71).aspx

The abstract modifier can be used with classes, methods, properties, indexers, and events.

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

Abstract classes have the following features:

  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Solution 4

Abstract classes should have at least one virtual method or property that has no implementation. This is marked with the abstract keyword. Inheriting classes must provide an implementation if they are not abstract themselves. You cannot create an instance of an abstract class because it does not have a complete implementation. If it does, it should not be marked abstract in the first place.

Solution 5

As an addition to the other answers, you may not be able to create an instance of the abstract class, but you can certainly refer to instances of derived types through the abstract type and use methods or properties that are defined within the abstract base.

abstract class A
{
    public abstract void D();
    public void E() { }
}

class B : A
{
    public override void D() { }
}

class C : A
{
    public override void D() { }
}

...

A a = new B();
a.D();
a.E();

List<A> list = new List<A>() { new B(), new C() };
Share:
96,961
Gaurav Arora
Author by

Gaurav Arora

Started with a passion to make career in medical line but moved into Software Engineering and started with C/C++. After getting into the ocean of programming moved to Java, afterward get introduced with new Microsoft Technologies .net and now-a-days making stuffs in .Net. I'm mentor at IndiaMentor: http://www.indiamentor.com/mentors/gaurav_kumar_arora

Updated on August 23, 2020

Comments

  • Gaurav Arora
    Gaurav Arora over 3 years

    Here is a scenario in my mind and I have googled, Binged it a lot but got the answer like

    "Abstract class has not implemented method so, we cant create the object" "The word 'Abstract' instruct the compiler to not create an object of the class"

    But in a simple class where we have all virtual method, able to create an object???

    Also, we can define different access modified to Abstract class constructor like private, protected or public.

    My search terminated to this question:

    Why we can't create object of an Abstract class?

  • Gaurav Arora
    Gaurav Arora about 14 years
    @Hunter Thanks, its right 'An abstract class cannot be instantiated.' But, why we can't create its object although it may contain public or protected constructors. Is there any logic or Framework restriction for the same?
  • vtortola
    vtortola about 14 years
    that is the real deal, the abstract methods :D
  • Gaurav Arora
    Gaurav Arora about 14 years
    @Mac Thanks for explaining the words But still the question stand alone : why we cant create an object of the class? Also the default modifier for constructor of Abstract class is Protected but we can define a public constructor then whats the use of public constructor.
  • Marc Gravell
    Marc Gravell about 14 years
    @Gaurav - in short, because that is how it is defined. In the case of abstract methods this is to provide sanity, but even without it makes no sense. If you want to be able to create instances - **don't mark it abstract **
  • Gaurav Arora
    Gaurav Arora about 14 years
    @Mac Thanks again for a great reply. Actually I will use a simple class in that case. But still I want to know, if Microsoft allows us to create a public constructor for Abstract classes then I think there is some reason behind this. Else why we can declare public constructor for Abstract classes as there is already protected constructor for Abstract class. I am bit confused here, if we can create private, protected or public constructors for Abstract classes then why we can't create the object?
  • Henk Holterman
    Henk Holterman about 14 years
    Wrong. There is nothing wrong with an abstract class w/o abstract members.
  • GalacticCowboy
    GalacticCowboy about 14 years
    The derived class can call the base constructor. Basically, with an "abstract" class you are telling the compiler "This is a concept, not a concrete object." Think "vehicle" vs. "Blue 2006 BMW 325i, serial number xxx".
  • GalacticCowboy
    GalacticCowboy about 14 years
    Also, you can still declare a variable of the abstract type, but it must be instantiate with one of its concrete descendants. vehicle myCar = new BMW325i();
  • Tom Cabanski
    Tom Cabanski about 14 years
    Interesting. Perhaps you can supply a reasonable example of where it makes sense to have an abstract class with no abstract methods or properties.
  • Mike Chess
    Mike Chess about 14 years
    Abstract classes are definitions of the protected and/or public methods that child classes must implement. It is a fundamental aspect of OOP. You can't create an instance of a class that has abstract methods in C# because there is no definition for what the abstract method should do. Some other languages, like Delphi 7, will allow instances of abstract classes to be created with predictable results when the instance tries to call an abstract method.
  • Henk Holterman
    Henk Holterman about 14 years
    Tom, Ok: class Car: Vehicle {}. Can you come up with a good case for having an instance of Vehicle?
  • Gaurav Arora
    Gaurav Arora almost 12 years
    if default constructor of abstract class would be public then it will allow to create an object anywhere then its the part of implemetation if we will get an error at that time So, I don't think this is the robust reason you explain. Sorry to make my hands dirty here :(
  • Gaurav Arora
    Gaurav Arora almost 12 years
    An abstract class has a protected constructor (by default) allowing derived types to initialize it.
  • Ravi Vanapalli
    Ravi Vanapalli over 11 years
    The framework has been defined such that when we use keyword "Abstract" with a class it cannot be instantiated but can only be inherited.
  • supercat
    supercat over 10 years
    @HenkHolterman: Vehicle isn't a great example, since it should almost certainly have some abstract members like Make and Model. On the other hand, one could have a class with two members which are each defined in terms of the other; some derived classes might override the first, others the second, and some both. For example, a type encapsulating a sequence might include ToArray and ToList methods, the first of which calls ToList().ToArray() and the second of which calls new List(ToArray()). Each would make sense as a default behavior if and only if the other is overridden.
  • Gaurav Arora
    Gaurav Arora over 10 years
    @Davesh - Acceptance of answer is within the limit of asked question. Your answer is surrounding to the accepted answer. This is by design and you are routing something to Virtual functions. Pure virtual functions are not within the limits of Abstract classes. I can't accept your answer, unless other Techies upgrade your answer.
  • user1781290
    user1781290 over 10 years
    This is actually an object of an anonymous class, not of the abstract one
  • rk rk
    rk rk over 10 years
    anonymous class means there should not be any name to the class.But here we have abstract class named person,how can this could be an anonymous class.
  • user1781290
    user1781290 over 10 years
    It is difficult to explain that in a comment. But there is a similar question here: stackoverflow.com/questions/5154533/…
  • Gaurav Arora
    Gaurav Arora over 10 years
    I agree this is difficult to explain in comments, this is not a best example for asked question. It could relate with anonymous types which is (in my view, might be others can oppose) by design. Well explained in msdn:msdn.microsoft.com/en-us/library/bb397696.aspx
  • Matti Virkkunen
    Matti Virkkunen over 9 years
    @Devesh: This isn't very language-agnostic. Many languages also allow for abstract classes with no abstract methods and an implementation detail such as a vtable has nothing to do with that.
  • Kelu Thatsall
    Kelu Thatsall over 9 years
    this way you are not exactly creating an object of class Example, but you are creating an object of anonymous class
  • raul
    raul about 9 years
    And this is what distinguishes extending abstract classes and inheritance: Objects of a subclass using the abstract class type can access methods introduced in the subclass, ie, methods that are not declared in the abstract class. Objects of a subclass using the superclass type (inheritance) cannot access methods introduced in the subclass.
  • Joffrey
    Joffrey almost 9 years
    The memory used by an instance of a class is not related to the implementation of the methods. The methods' code takes up the space related to the Class itself, not the objects of that class. What point would there be in storing the code of the methods in every object?
  • Joffrey
    Joffrey almost 9 years
    Sorry but no. You're creating an instance of an anonymous inner class created on-the-fly. Try to use getClass() on your ob object, you'll see.
  • Joffrey
    Joffrey almost 9 years
    There is a way to explain it simply. The runtime class of p is itself created on-the-fly as an anonymous inner class extending Person, with the given methods. If you use getClass() on your object, it won't return the Person class object, but a class with the name Helloworld$1 (1st anonymous inner class of Helloworld).
  • Joffrey
    Joffrey almost 9 years
    Please use proper language, your post is annoying to read. That's probably why you got downvoted despite a content that could make sense.
  • VINOTH ENERGETIC
    VINOTH ENERGETIC about 7 years
    @Marc Gravell Why compiler is not blocking when we make constructor as public for abstract class?
  • Marc Gravell
    Marc Gravell about 7 years
    @Vinoth it is a fair question - ultimately protected would have served just fine. Maybe it just wasn't worth the hassle of enforcing it?
  • VINOTH ENERGETIC
    VINOTH ENERGETIC about 7 years
    @MarcGravell okay. Just to inform you that I checked by keeping constructor as public for abstract class. Compiler is throwing error when we attempt to create object for class.
  • Marc Gravell
    Marc Gravell about 7 years
    @VINOTHENERGETIC well, yes, of course it would - it is abstract - it can't be created; that doesn't change anything I said...