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.
Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.
Abstract methods have the following features:
- An abstract method is implicitly a virtual method.
- Abstract method declarations are only permitted in abstract classes.
- Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature. For example:
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.
Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractCLass
{
class AbstractClassDemo
{
public abstract void Animal(string strAnimal)
{
Console.WriteLine("Animal Name is :-" +strAnimal);
}
}
}
when u compile this u ll get error as
Can not declare a body because it marked as Abstract
Instead u use following code to make a abstract Function.
Second thing is class is not abstract so we have to make it abstract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractCLass
{
abstract class AbstractClassDemo
{
public abstract void Animal(string strAnimal);
}
}
it will compile
Now u Run below Code.
1) Class 1
namespace AbstractCLass
{
abstract class AbstractClassDemo
{
public abstract void Animal(string strAnimal);
}
class Derived_abstract : AbstractClassDemo
{
public override void Animal(string strAnimal)
{
Console.WriteLine("This animal is := "+strAnimal);
}
}
}
2) Class 2
namespace AbstractCLass
{
class Class_Main
{
static void Main(string[] args)
{
Derived_abstract objDerived_abstract = new Derived_abstract();
objDerived_abstract.Animal("C# .Net");
Console.ReadLine();
}
}
}
This is a simple Demo for abstract Class
I also Refer Some concepts of Microsoft MSDN
Its helps me Lot to clear My Concepts
No comments:
Post a Comment