SIGN UP MEMBER LOGIN:    
ARTICLE

Abstract Classes And Methods

Posted by Rajesh VS Articles | C# Language October 08, 2001
This is a detailed analysis of Abstract classes and methods in C# with some concrete examples.
Reader Level:

This is a detailed analysis of Abstract classes and methods in C# with some concrete examples.

The keyword abstract can be used with both classes and methods in C# to declare them as abstract.

The classes, which we can't initialize, are known as abstract classes. They provide only partial implementations. But another class can inherit from an abstract class and can create their instances.

For example, an abstract class with a non-abstract method.

using System;
abstract class MyAbs
{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
}
class MyClass : MyAbs
{
}
class MyClient
{
public static void Main()
{
//MyAbs mb = new MyAbs();//not possible to create an instance
MyClass mc = new MyClass();
mc.NonAbMethod();
// Displays 'Non-Abstract Method'
}
}

An abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class.

An abstract method is a method without any method body. They are implicitly virtual in C#.

using System;
abstract class MyAbs
{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
public abstract void AbMethod(); // An abstract method
}
class MyClass : MyAbs//must implement base class abstract methods
{
public override void AbMethod()
{
Console.WriteLine("Abstarct method");
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.NonAbMethod();
mc.AbMethod();
}


But by declaring the derived class also abstract, we can avoid the implementation of all or certain abstract methods. This is what is known as partial implementation of an abstract class.

using System;
abstract class MyAbs
{
public abstract void AbMethod1();
public abstract void AbMethod2();
}
//not necessary to implement all abstract methods
//partial implementation is possible
abstract class MyClass1 : MyAbs
{
public override void AbMethod1()
{
Console.WriteLine("Abstarct method #1");
}
}
class MyClass : MyClass1
{
public override void AbMethod2()
{
Console.WriteLine("Abstarct method #2");
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.AbMethod1();
mc.AbMethod2();
}
}

In C#, an abstract class can inherit from another non-abstract class. In addition to the methods it inherited from the base class, it is possible to add new abstract and non-abstract methods as showing below.

using System;
class MyClass1 // Non-Abstract class
{
public void Method1()
{
Console.WriteLine("Method of a non-abstract class");
}
}
abstract class MyAbs : MyClass1 // Inherits from an non-abstract class
{
public abstract void AbMethod1();
}
class MyClass : MyAbs//must implement base class abstract methods
{
public override void AbMethod1()
{
Console.WriteLine("Abstarct method #1 of MyClass");
}
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.Method1();
mc.AbMethod1();
}

 
An abstract class can also implement from an interface. In this case we must provide method body for all methods it implemented from the interface.

using System;
interface IInterface
{
void Method1();
}
abstract class MyAbs : IInterface
{
public void Method1()
{
Console.WriteLine("Method implemented from the IInterface");
}
}
class MyClass : MyAbs//must implement base class abstract method
{
}
class MyClient
{
public static void Main()
{
MyClass mc =
new MyClass();
mc.Method1();
}
}
}


We can't use the key word abstract along with sealed in C#, since a sealed class can't be abstract.

The abstract methods are implicitly virtual and hence they can't mark explicitly virtual in C#.

For example

using System;
abstract class MyAbs
{
public abstract void AbMethod1();
public abstract void AbMethod2();
}
class MyClass1 : MyAbs
{
public override void AbMethod1(){
Console.WriteLine("Abstarct method #1 of MyClass1");
}
public override void AbMethod2()
{
Console.WriteLine("Abstarct method #2 of MyClass1");
}
}
class MyClient
{
public static void Main()
{
MyAbs ma1 =
new MyClass1();// Polymorphism
ma1.AbMethod1();
ma1.AbMethod2();
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

thx.... so helpful

Posted by Reesky Adhadina Jun 26, 2011

When to use Interface & when to use Inheritance???

Posted by Max Dekhane Feb 27, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor