Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - ABSTRACT CLASSES


An abstract class in C# is a class which cannot be instantiated and can have methods which are not defined.

  1. To make a class abstract, we need to place the abstract keyword just before the class definition.

  2. And as said above, there can be one or more undefined method which must have abstract keyword before the method declaration.

  3. We cannot create objects of an Abstract class.

  4. The classes extending an abstract class has to define the undefined methods(i.e. the abstract methods).


We will clear the above points with the example :


Story :

The supreme god decided that in order to communicate, every living being should make some sound. i.e Humans will talk, dogs will bark, cats meao e.t.c.

Now, there was no way he could define the sound behavior as it will be different for all living beings. So he made this rule that every living being should have a sound behavior but the sound they makes will be determined by their own gods. And thus the class was prepared by Master God:

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:

  1. Before the sound() behaviour(As it is incomplete).



    abstract void sound();

  2. Before the LivingBeing class(Since sound() is incomplete, the class is incomplete as well).



    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:


Human Being Creator


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.
	}
}



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 :

So, the Human creator god is bound to define the sound() method/behaviour as he is extending the LivingBeing class. Naturally the purpose of the supreme god was solved. As he wanted all living beings should make some sound. What kind of sound? will be determined by their own Gods.

If the Human creator god wouldn't have defined the sound() method/behaviour the code would have ended up with some kind of error.

Now, let us write the complete code for the above example :


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();
	}
}


Output :



  Breathes oxygen.
  Humans will speak

Note : There is no hard and fast rule that at least one method has to be abstract in an abstract class. An abstract class can contain all methods which are defined(i.e. non abstract methods) but if there is at least one method in a class marked abstract, the class should be abstract as well.