An abstract class is a class which provides a common definition of base class that multiple derived classes can share.
Some distinct characteristics of an abstract class:
Ø We cannot create an object of the abstract class, it must be inherited.
Ø It can have abstract as well as non-abstract members in an abstract class.
Ø We must declare at least one abstract method in the abstract class.
Ø An abstract class is always public.
We need abstract classes whenever we define a template that needs to be followed by all the derived classes.
Abstract classes are specified by keyword Abstract
Example:
Abstract class A
{
Public abstract void display ();
}
Class B : A
{
Public override void display ()
{
Console.writeLine (“this is class B’s method”);
}
Class C : A
{
Public override void display ()
{
Console.writeLine (“this is class C’s method”);
}
Class program
{
Public static void main (string [] args)
{
B b=new B ();
b.display ();//calls class B’s method
C c=new C ();
c.display ();// calls class C’s method
}
}
No comments:
Post a Comment