Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//An Abstract class may have public constructor but we can not able to create its object.

public abstract class MyAbstract
{
private int x;
public MyAbstract(int p)
{
this.x = p;
}
}

public class MyClass : MyAbstract
{
public MyClass(int p) : base(8)
{
p = 8;
}
}

public class Hello
{
MyAbstract abss = new MyClass(3);
MyAbstract abss = new MyAbstract(4);
MyClass cls = new MyAbstract(5);
}

In the above example last two line is not permissible.
Posted
Comments
[no name] 23-Aug-14 2:01am    
simply abstract class is designed for that

It cannot be done and makes no sense. The whole purpose of the keyword abstract, for a type, is to prevent its instantiation. Note, I say "purpose of the keyword", not abstract classes. Then it brings us the the second question: why having abstract classes at all?
Only for one purpose: to be used as a base classes for other classes; but ultimately, some of those classes should be made non-abstract, otherwise the whole activity would lack the purpose.

Such abstract classes can be used as the compile-time types used for access to some polymorphic sets. To understand that, you need to understand the heart of OOP: virtual methods and late binding. It includes understanding of compile-time types vs. run-time types. When you have some class instance, it can be accessible through some variable or a member of some compile-time type, which can be abstract, but its runtime type is always non-abstract, otherwise you could not have an instance. Such abstract types are used as a common interface for all members of the set, having different runtime types. And if this type is not abstract, no functionality will change: abstract is a kind of fool-proof measure, just to prevent unwanted instantiation by some developers who might not understand the purpose of this type.

—SA
 
Share this answer
 
Comments
[no name] 23-Aug-14 2:01am    
nice description my vote for u sir
Sergey Alexandrovich Kryukov 23-Aug-14 2:19am    
Thank you very much, Sankarsan.
—SA
Protected might be the way that the framework enforces the mechanism. But the framework is preventing you from doing something that doesn't make sense.

An abstract class is a class that contains some methods that are not yet implemented. They must be implemented by a derived 'concrete' class. As such, we can say that an abstract class is 'incomplete'. If you were to instantiate one, it will have 'holes' where those methods are. The framework is preventing you from building a class with holes in it. Hence you can only instantiate a concrete class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900