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.
In this example, we will create a base class for four legged animals and then create a Dog class, which inherits from it, like this:
namespace
AbstractClasses
{
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Console.WriteLine(dog.Describe());
Console.ReadKey();
}
}
abstract class FourLeggedAnimal
{
public virtual string Describe()
{
return "Not much is known
about this four legged animal!";
}
}
class Dog : FourLeggedAnimal
{
}
}
If you compare it with the examples in the chapter about inheritance, you won't see a big difference. In fact, the abstract keyword in front of the FourLeggedAnimal definition is the biggest difference. As you can see, we create a new instance of the Dog class and then call the inherited Describe() method from the FourLeggedAnimal class.
Example 2:
abstract class FourLeggedAnimal
{
public virtual string Describe()
{
return "This animal has four legs.";
}
}
class Dog : FourLeggedAnimal
{
public override string Describe()
{
string result = base.Describe();
result += " In fact, it's a dog!";
return result;
}
}
Abstract classes may also define
abstract methods. This is accomplished by adding the keyword abstract before
the return type of the method. For example:
C#
public abstract class A
{
public abstract void DoWork(int i);
}
Abstract methods have no
implementation, so the method definition is followed by a semicolon instead of
a normal method block. Derived classes of the abstract class must implement all
abstract methods. When an abstract class inherits a virtual method from a base
class, the abstract class can override the virtual method with an abstract
method. For example:
C#
// compile with: /target:library
public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}
public abstract class E : D
{
public abstract override void DoWork(int i);
}
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}
If a virtual method is declared
abstract, it is still virtual to any class inheriting from the abstract class.
A class inheriting an abstract method cannot access the original implementation
of the method—in the previous example, DoWork on class F cannot call DoWork on
class D. In this way, an abstract class can force derived classes to provide
new method implementations for virtual methods.
Example to understand difference between normal method and
abstract method
class Program
{
public static void Main()
{
Alto
objmn = new Alto();
objmn.method();
objmn.abmethod();
Console.ReadLine();
}
}
abstract class Maruti
{
//Constructor
public
Maruti()
{
Console.WriteLine("Maruti is a car manufacturing company");
}
//Normal
Method
public void method()
{
Console.WriteLine("This is normal method");
}
//Abstract
method
abstract
public void
abmethod();
}
class Alto : Maruti
{
public override void
abmethod()
{
Console.WriteLine("This is abstract method");
}
}
The only difference is that an abstract base class cannot be
instantiated without being derived from, whereas a non-abstract one can. From
the point of view of a derived class, everything is the same.
Normal Class
|
Abstract Class
|
class Program
{ public static void Main() { Class1 ObjCls = new Class1(); ObjCls.sleep(); Cat ObjCat = new Cat(); ObjCat.sleep(); Dog objDog = new Dog(); objDog.sleep(); Console.ReadLine(); } } class Class1 { public void sleep() { Console.WriteLine("This is sleep method "); } } class Cat:Class1 { } class Dog : Class1 { } |
class Program
{ public static void Main() { Cat ObjCat = new Cat(); ObjCat.sleep(); Dog objDog = new Dog(); objDog.sleep(); Console.ReadLine(); } } abstract class Class1 { public void sleep() { Console.WriteLine("This is sleep method"); } } class Cat:Class1 { } class Dog : Class1 { } |
Output
|
Output
|
This is sleep method
This is sleep method This is sleep method |
This is sleep method
This is sleep method |
No comments:
Post a Comment