About Me

Hyderabad, Andhra Pradesh, India

Monday, July 11, 2011

Abstract Class

A class which cannot be instantiated is known as Abstract class.That is an object of it cannot be created.Hence it must be inherited.Below are some of the features of an Abstract class:

1. Abstract Class cannot be instantiated.
2. An Abstract Class must be inherited.
3. It may have Concrete Methods.
4. It may have Abstract Methods.
5. An Abstract Method of an Abstract Class must be overridden.
6. An Abstract Class can only contain Abstract Method.

Abstract Class cannot be instantiated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExAbstract
{

abstract class sample
{
public sample()
{
Console.WriteLine( "Constructor of the Abstract Class" );
}
}
class Program
{
static void Main( string[] args )
{
sample p = new sample();
}
}
}

Error Message




An Abstract Class must be inherited

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExAbstract

{
abstract class A
{
public A()
{
Console.WriteLine( "Abstract class Constructor" );
}
}
class B : A
{

}
class Program
{
static void Main( string[] args )
{
//A obj = new A();
B obj = new B();
Console.ReadLine();
}
}
}


Output:



Abstract Class may have Concrete Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExAbstract
{
abstract class A
{
public A()
{
Console.WriteLine( "Abstract class Constructor" );
}
public void display()
{
Console.WriteLine( "This is a concrete method in Abstract Class" );
}
}
class B : A
{

}
class Program
{
static void Main( string[] args )
{
//A obj = new A();
B obj = new B();
obj.display();
Console.ReadLine();
}
}
}

Output:



Abstract Class may have Abstract Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExAbstract
{
abstract class A
{
public A()
{
Console.WriteLine( "Abstract class Constructor" );
}
public void display()
{
Console.WriteLine( "This is a concrete method in Abstract Class" );
}
public abstract void display2();
}
class B : A
{

}
class Program
{
static void Main( string[] args )
{
//A obj = new A();
B obj = new B();
obj.display();
Console.ReadLine();
}
}
}

Error Message



An abstract method of an abstract class must be overridden

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExAbstract
{
abstract class A
{
public A()
{
Console.WriteLine( "Abstract class Constructor" );
}
public void display()
{
Console.WriteLine( "This is a concrete method in Abstract Class" );
}
public abstract void display2();
}
class B : A
{
public override void display2()
{
Console.WriteLine( "An Abstract method is being overriden here" );
}
}
class Program
{
static void Main( string[] args )
{
//A obj = new A();
B obj = new B();
obj.display();
obj.display2();
Console.ReadLine();
}
}
}


An Abstract Method can be present only in Abstract Class

No comments:

Post a Comment