public abstract class car
{
public car() // abstract class constructor
{
Console.WriteLine("Abstract Class constructor");
}
public int noofWheels = 4; // non abstract fields
public int speed = 60; // non abstract fields
public int getSpeed() // non abstract method
{
return speed;
}
public abstract void features(); // abstract method
}
public class Audi : car
{
public Audi()// inherited class constructor
{
Console.WriteLine("Audi Class constructor");
}
private string carType = "Audi";
public override void features() // abstract method overriding
{
Console.WriteLine(carType);
}
}
class Program
{
static void Main(string[] args)
{
Audi a = new Audi();
a.features();
Console.ReadLine();
}
}
public interface Icar
{
void features();
string manufacturer();
}
public class Audi : Icar
{
public Audi()// inherited class constructor
{
Console.WriteLine("Audi Class constructor");
}
private string carType = "Audi A8";
private string carManufacturer = "Audi";
public override void features()
{
Console.WriteLine(carType);
}
public string manufacturer()
{
return carManufacturer;
}
class Program
{
static void Main(string[] args)
{
Audi a = new Audi();
a.features();
Console.WriteLine(a.manufacturer());
Console.ReadLine();
}
}
Output
- Abstract Class: Non abstract methods are allowed with in the class Interface: All member methods are abstract
- Abstract Class: Both methods are fields are allowed with in an abstract class Interface: Only methods are allowed
- Abstract Class: Can't inherit multiple abstract classes together with in a class, thus abstract class doesn't allow multiple inheritance Interface: Can inherit multiple interfaces with in a class, multiple inheritance in C# can only achieved through Interface
- Abstract Class: Can have constructor in an abstract class Interface: Interface doesn't have constructor as its interface provides only contract with the inherited class not actual memory allocation takes place in interface.
- Abstract Class: Abstract class might not provide multiple inheritance but it doesn't affect the basic architecture of a design Interface: Interface provides multiple inheritance but its a weak form of multiple inheritance so in case of complex projects it could affect the architecture of the application.