views:

472

answers:

7

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 clr that not to create 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?

+23  A: 

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.

Marc Gravell
+1 excellent example
Andrey
that is the real deal, the abstract methods :D
vtortola
Well put good sir.
Jon Ownbey
@MacThanks 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.
Rick
@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` **
Marc Gravell
@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?
Rick
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
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();**
GalacticCowboy
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.
Mike Chess
+1  A: 

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.
hunter
@HunterThanks, 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?
Rick
+10  A: 

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?

froadie
+1  A: 

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.

Tom Cabanski
Wrong. There is nothing wrong with an abstract class w/o abstract members.
Henk Holterman
Interesting. Perhaps you can supply a reasonable example of where it makes sense to have an abstract class with no abstract methods or properties.
Tom Cabanski
Tom, Ok: `class Car: Vehicle {}`. Can you come up with a good case for having an _instance_ of Vehicle?
Henk Holterman
A: 

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() };
Anthony Pegram
A: 

Simply speaking, an abstract class is like a shell of a class. Not all the methods have implementations, something like a circuit with some wire segments missing. While the majority of it may be constructed, it is up to the users to stick in the wires and resistors in those segments as they see fit.

As to why Java won't let you create it, part of it is just a failsafe (many abstract classes will function just fine without any additions as long as you don't call unimplemented methods).

piggles
A: 

Here is a similar StackOverflow question. In short, it is legal to have a public constructor on an abstract class. Some tools will warn you that this makes no sense.

http://stackoverflow.com/questions/1682275/whats-the-utility-of-public-constructors-in-abstract-classes-in-c

Paul Williams