Home | Articles|Namespace|Interview Questions|Tools|Jobs|Projects|Community
Asp.net Tutorials

»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books

»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects

»Competition
Community
Winners
Prizes
Write For Us
Members

»Other Resources
Links
Dotnet Resources

Abstract Classes And Methods

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 AbMet

Have a Question and dont know the answer post it below and get answers in minutes

Due to spam this feature is disabled
To get answers fast , make sure you enter a detailed subject for example: "DataGrid issues need answer" not "DataGrid"

Subject:

Catjegory Name:

Message:



© 2008 dotnetwatch.com -- Privacy policy