What is an Abstract class in C#?
1.It is special kind of class that has implementation, we cannot instantiated(object).
2.It is the base class of derived class and it give functionality to derived or sub class.
3.Without Implementation of method is seem to be like an Interface but it doesn't support Multiple Inheritance.
4.It can have both (Abstract method and non Abstract method).
ex:- public abstract void sample(); // Abstract Method
public void sample(){//---} //Non Abstract Method with Implementation.
Program
> Abstract class with non Abstract Method;
using System;
using System.Text;
namespace Test_Application
{
abstract class Vehicle
{
public void Car()
{
Console.WriteLine("Car is four wheeler");
}
}
class Program : Vehicle
{
public static void Main(string[] args)
{
Program v = new Program(); //
v.Car();
Console.ReadLine();
}
}
}
Output :- Car is four wheeler
Note :- Here we cannot create Vehicle as object instead of Program because it will show compilation error .
Vehicle p = new Vehicle => Program v = new Program();
>Abstract class with an Abstract Method.
using System;
using System.Text;
namespace Test_Application
{
abstract class Vehicle
{
public abstract void Bicycle();
}
class Program : Vehicle
{
public override void Bicycle()
{
Console.WriteLine("Bicycle is Two wheeler");
}
public static void Main(string[] args)
{
Program v = new Program();
v.Bicycle();
Console.ReadLine();
}
}
}
Output:- Bicycle is Two wheeler
Note : While create an Abstract Method in abstract class :-
1 abstract void Bicycle(); // compilation error because this considered as private
virtual or abstract member cannot be private.
2 public abstract void Bicycle(); // Is considered.
3 In derived class => public override void Bicycle(){ ----};
override keyword must required.
> Abstract class with Abstract Method and non Abstract Menthod;
using System;
using System.Text;
namespace Test_Application
{
abstract class Vehicle
{
public void Car()
{
Console.WriteLine("Car is a four wheeler");
}
public abstract void Bicycle();
}
class Program : Vehicle
{
public override void Bicycle()
{
Console.WriteLine("Bicycle is a Two wheeler");
}
public static void Main(string[] args)
{
Program v = new Program();
v.Car();
v.Bicycle();
Console.ReadLine();
}
}
}
Output:- Car is a four wheeler
Bicycle is a Two wheeler
1.It is special kind of class that has implementation, we cannot instantiated(object).
2.It is the base class of derived class and it give functionality to derived or sub class.
3.Without Implementation of method is seem to be like an Interface but it doesn't support Multiple Inheritance.
4.It can have both (Abstract method and non Abstract method).
ex:- public abstract void sample(); // Abstract Method
public void sample(){//---} //Non Abstract Method with Implementation.
Program
> Abstract class with non Abstract Method;
using System;
using System.Text;
namespace Test_Application
{
abstract class Vehicle
{
public void Car()
{
Console.WriteLine("Car is four wheeler");
}
}
class Program : Vehicle
{
public static void Main(string[] args)
{
Program v = new Program(); //
v.Car();
Console.ReadLine();
}
}
}
Output :- Car is four wheeler
Note :- Here we cannot create Vehicle as object instead of Program because it will show compilation error .
Vehicle p = new Vehicle => Program v = new Program();
>Abstract class with an Abstract Method.
using System;
using System.Text;
namespace Test_Application
{
abstract class Vehicle
{
public abstract void Bicycle();
}
class Program : Vehicle
{
public override void Bicycle()
{
Console.WriteLine("Bicycle is Two wheeler");
}
public static void Main(string[] args)
{
Program v = new Program();
v.Bicycle();
Console.ReadLine();
}
}
}
Output:- Bicycle is Two wheeler
Note : While create an Abstract Method in abstract class :-
1 abstract void Bicycle(); // compilation error because this considered as private
virtual or abstract member cannot be private.
2 public abstract void Bicycle(); // Is considered.
3 In derived class => public override void Bicycle(){ ----};
override keyword must required.
> Abstract class with Abstract Method and non Abstract Menthod;
using System;
using System.Text;
namespace Test_Application
{
abstract class Vehicle
{
public void Car()
{
Console.WriteLine("Car is a four wheeler");
}
public abstract void Bicycle();
}
class Program : Vehicle
{
public override void Bicycle()
{
Console.WriteLine("Bicycle is a Two wheeler");
}
public static void Main(string[] args)
{
Program v = new Program();
v.Car();
v.Bicycle();
Console.ReadLine();
}
}
}
Output:- Car is a four wheeler
Bicycle is a Two wheeler