· Class cannot be instantiated but only used as based class.
· But another class can inherit from an abstract class and can create their instances
· we can implement methods, fields, and properties in the abstract class that can be used by the child class
· Abstract class can't be static.
· Abstract class is used for inheritance.
· Abstract class can't be sealed.
· Abstract or virtual members can't be private, as we can't override it.
· Abstract class can be inherited from an abstract class but the methods in the base class have to be declared abstract.
· Abstract method and properties do not have any functionality. The derived class defines their full functionality.
Ex:
namespace abstract_class
{
class Program
{
public abstract class clsAbstract
{
public abstract int sum(int a, int b);
public abstract int subt(int a, int b);
public void Display()
{
Console.WriteLine("Abstract class");
}
}
public class clsDerived : clsAbstract
{
// Method overriding
public override int subt(int a, int b)
{
return (a - b);
}
// Method overriding
public override int sum(int a, int b)
{
return (a + b);
}
public void Show()
{
Console.WriteLine("YY class method");
}
}
static void Main(string[] args)
{
clsDerived obj = new clsDerived ();
// here you can't create instance of a abstract class it is giving error
// clsDerived obj1 = new clsAbstract ();
// clsAbstract obj2 = new clsAbstract ();
// here we have used up level casting
clsAbstract obj3 = new clsDerived ();
// here you don't have access to the Show method of YY
// Because you can only access methods of a reference object only
//obj3.Show();
Console.WriteLine("Calling sum method through reference of base class " + obj3.sum(2,2));
obj.Display();
Console.WriteLine("Subtraction : "+ obj.subt(12, 10));
Console.WriteLine("Addition : " + obj.sum(10, 10));
Console.ReadLine();
}
}
}
Related Posts
Abstract Class In C#
Sealed Class In C#
Interface In C#
Abstract Class vs. Interface in C#
NET framework Comparison
C# Interview Questions
No comments:
Post a Comment