An abstract class in C# is a class which cannot be instantiated and can have methods which are not defined.
We will clear the above points with the example :
Story :
abstract class LivingBeing { void breathe() { System.Console.WriteLine("Breathes oxygen."); } abstract void sound(); // The sound method is incomplete. }
Now we can see the abstract keyword used in two places:
abstract void sound();
abstract class LivingBeing
So, from the above definition it is pretty clear that if a method/behaviour is not defined it should be marked as abstract.
Thus the LivingBeing class is also Abstract because it has an abstact behaviour(Incomplete behaviour).
And since the class is incomplete we cannot create any objects out of it.
In other words abstract classes cannot be instantiated.
Then, what is the purpose of this class from which we cannot create any objects? We will see it below:
abstract class LivingBeing { public void breathe() { System.Console.WriteLine("Breathes oxygen."); } public abstract void sound(); // The sound method is incomplete. } class Human : LivingBeing { string name; string food; string language; public Human() { } public Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } public void eat() { System.Console.WriteLine("Eats "+food); } public void speak() { System.Console.WriteLine("Speaks "+language); } public override void sound() { System.Console.WriteLine("Humans will speak"); // This method has to be defined. } }
So, in the above example we have an abstract class LivingBeing.
abstract class LivingBeing { public void breathe() { System.Console.WriteLine("Breathes oxygen."); } public abstract void sound(); // The sound method is incomplete. }
So, as we know the child classes that inherits from it must define the abstract method sound().
public abstract void sound();
And so we have done in the Human class.
class Human : LivingBeing { string name; string food; string language; public Human() { } public Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } public void eat() { System.Console.WriteLine("Eats "+food); } public void speak() { System.Console.WriteLine("Speaks "+language); } public override void sound() { System.Console.WriteLine("Humans will speak"); // This method has to be defined. } }
We have defined the sound() method using the override keyword,
public override void sound() { System.Console.WriteLine("Humans will speak"); // This method has to be defined. }
Story :
Now, let us write the complete code for the above example :
abstract class LivingBeing { public void breathe() { System.Console.WriteLine("Breathes oxygen."); } public abstract void sound(); // The sound method is incomplete. } class Human : LivingBeing { string name; string food; string language; public Human() { } public Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } public void eat() { System.Console.WriteLine("Eats "+food); } public void speak() { System.Console.WriteLine("Speaks "+language); } public override void sound() { System.Console.WriteLine("Humans will speak"); // This method has to be defined. } } class CreateLivingBeing { public static void Main(string[] args) { Human human = new Human("John", "Burger", "English"); human.breathe(); human.sound(); } }