Abstract Class

Ganeshji
Posted by in C# category on for Beginner level | Points: 250 | Views : 50268 red flag
Rating: 4.5 out of 5  
 2 vote(s)

The class which can not be instantiated is known as an Abstract Class. This particular article contains the details of an Abstract Class.


 Download source code for Abstract Class


Introduction


The class which cannot be instantiated is known as an Abstract Class. That is, an object of it cannot be created. Hence it must be inherited. Let me cite some of the features of an Abstract Class.

Features of an Abstract Class


1. Abstract Class cannot be instantiated.
2. An Abstract Class must be inherited.
3. It may have Concrete Methods.
4. It may have Abstract Methods.
5. An Abstract Method of an Abstract Class must be overridden.
6. An Abstract Class can only contain Abstract Method.



Abstract Class cannot be instantiated.




Error Message





It must be inherited






Output





Abstract Class may have Concrete Method





Output





Abstract Class may have Abstract Method






Error Message





An abstract method of an abstract class must be overridden




Output





An Abstract Method can be present only in Abstract Class







Error Message







Hope this article will be useful.

Thanks!
Page copy protected against web site content infringement by Copyscape

About the Author

Ganeshji
Full Name: Zinnia Sarkar
Member Level:
Member Status: Member
Member Since: 7/24/2010 12:50:40 PM
Country: India
Regards, Ganeshji


Login to vote for this post.

Comments or Responses

Posted by: Madhu.b.rokkam on: 2/17/2011 | Points: 25
Good one .. :)
Posted by: Karthikanbarasan on: 2/17/2011 | Points: 25
Nice article for the beginers
Posted by: Tripati_tutu on: 2/17/2011 | Points: 25
I think this is good for new learners.
Posted by: Kishork80 on: 2/17/2011 | Points: 25
Hey,
The statement in the last point 'An Abstract Class can only contain Abstract Method is ambiguous.'
What I say is ,
An abstract class can have a Normal method as well as an abstract methods.
That means If any one method inside a class is abstract the WHOLE CLASS has to be abstract but not vice versa.




Posted by: Ganeshji on: 2/17/2011 | Points: 25
Thnx Kishork80.
Posted by: Venu_msis on: 2/17/2011 | Points: 25
Nice Article!
Posted by: Karthikanbarasan on: 2/17/2011 | Points: 25
Can you post similarly for Interface, Encapsulation and Abstraction with examples? it will be really helpful.
Posted by: Ganeshji on: 2/17/2011 | Points: 25
Yes, Karthikanbarasan I have posted a similar article on Interface today. Rest i shall cover asap. Plz keep on suggesting me in this way. Many Thnx!
Posted by: Pradsir on: 2/17/2011 | Points: 25
Ganesh ji..

Thanks for Submitting very good articles..
I have got few quick Q ..

can we have seald methods in Abstract Class ?
can we have Virtual methods in Abstract Class ?

Thanks.



Posted by: Karthikanbarasan on: 2/17/2011 | Points: 25
Nice Ganeshji.... Its really helpful... Keep going....
Posted by: Bcs2002 on: 2/18/2011 | Points: 25
Hi
Good article. But when and where should you user abstract classes and what is the use of abstract classes?
thanks
bcs
Posted by: Kishork80 on: 2/18/2011 | Points: 25
@Karthikanbarasan

We can not have 'Sealed' type because sealed contradicts the purpose of 'Abstract'. On the other side we can haev virtual because it can be overriden in the implemeting class.

See the code below.
abstract class TTest
{
public virtual void T1()
{ }
//ERROR:'ConsoleApplication2.TTest.T2()' cannot be sealed because it is not an override
public sealed void T2()
{ }
}

Posted by: Pradsir on: 2/18/2011 | Points: 25
@ KishorK80..

Thanks kishor..

But i think we need to check the below line of code.

using System;
class Program
{
static void Main(string[] args)
{ C c1 = new C();
c1.Hello();

}

}
class A
{
public virtual void Hello()
{
Console.WriteLine(" Say Hello");
}

}

abstract class B : A
{
public sealed override void Hello()
{
Console.WriteLine(" Say Hi");
}

}

class C : B
{
}


//Output is
// Say Hi
//Press any key to continue . . .


Here I was able to declare Sealed Method in Class B which is an abstract class.

Thanks,


Posted by: Pradsir on: 2/18/2011 | Points: 25
@Bcs2002

Abstract class can be used in order to create the looseley coupled environment.

We can put all common set of functionality & Behaviour in the abstact class..
& then we can derive the abstract class implementing abstract methods..

Thanks,
Posted by: Akiii on: 8/15/2011 | Points: 25
Very good explanation......
just can you tell me what is the sole purpose of an abstract class ?

Thanks and Regards
Akiii
Posted by: Ganeshji on: 8/18/2011 | Points: 25
Yes Akii! Very Good Question u have. If anyone wishes to make use of a particular function signature, then abstract class can be ideally used.
Posted by: Kamlesh420 on: 12/20/2011 | Points: 25
nice and simple article thanks

can u also plz define interface
Posted by: Ganeshji on: 12/20/2011 | Points: 25
Thanks Kamlesh. I have an article on interface in the following link.http://www.dotnetfunda.com/articles/article1184-an-interface-in-csharp-.aspx. Enjoy Reading!
Posted by: Vijayakumarp on: 3/19/2012 | Points: 25
Nice one Zinnia Sarkar.. Thanks for Great explanation...
Posted by: Ganeshji on: 3/19/2012 | Points: 25
Thanks Vijayakumarp!

Login to post response

Comment using Facebook(Author doesn't get notification)