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




C# - INHERITANCE


Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.


Story :

God has created human beings. So, he has prepared the 'Human' class with states and behaviours. So that human beings could be created based on the stated behaviour.

Now let's say there is a supreme God(more powerful than the God who created Humans). He had decided how all living beings should behave(Human, Animal, Fish).

So, he created LivingBeing class.


class LivingBeing
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from air.");
	}
}

Story :

Now, the Supreme God(i.e. The creator of the above 'LivingBeing' class) made this rule that all living beings, be it Human or Animal everyone should breathe oxygen from air.

So, the God who has created Human is not going to define the breathe() behaviour again. And he decided to reuse it.

Now, LivingBeing is the parent class and Human class is going to be the child class.


Let us see in the below example.


Example :



class LivingBeing 
{
    public void breathe() 
    {
        System.Console.WriteLine("Breathes oxygen from air");
    }
}

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(name+" eats "+food);
    }

    public void speak() 
    {
        System.Console.WriteLine(name+" speaks "+language);
    }
}

class CreateHuman
{
	public static void Main(string[] args)
	{
        Human human1 = new Human("John", "Burger", "English");

        human1.eat();
        human1.speak();

        human1.breathe();
	}
}


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from air

So, in the above example, we have created a LivingBeing class that just has a Behaviour/Method i.e. breathe().


class LivingBeing
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from air");
	}
}
C_Sharp


Then we have created the Human class and the Human class should have the breathe() method from the LivingBeing class.


And since, the breathe() method is defined in the LivingBeing class. There is no point in redefining the breathe() method again.


Rather the Human class reuses it by inheriting all the members of the LivingBeing class.


class Human : LivingBeing

The syntax is quite simple. We just place the class to be inherited(i.e. LivingBeing) followed by colon(i.e. :).

C_Sharp

And what happens is, breathe() method of LivingBeing class becomes a part of the Humanclass.

C_Sharp

And along with eat() and speak() method, breathe() method also becomes a part of Human class.


Now, after creating the human1 object,


Human human1 = new Human("John", "Burger", "English");
C_Sharp


Now when we call the breathe() method from human1 object.


human1.breathe()

We get the below output.


Breathes oxygen from air

Use of base Keyword


The base Keyword is used in the child class to call the Constructor of its Parent class.


To understand base Keyword, let us take the example of secondary constructor to achieve it.


Let's simplify with the below example.


Example :



class LivingBeing 
{
    string source;
    
    public LivingBeing()
    {
        
    }
    
    public LivingBeing(string sourceTemp)
    {
        source = sourceTemp;
    }

    public void breathe() 
    {
        System.Console.WriteLine("Breathes oxygen from "+source);
    }
}

class Human : LivingBeing 
{
    string name;
	string food;
	string language;
	
	public Human()
	{

	}

	public Human(string nme, string fd, string lang, string sourceTemp) : base(sourceTemp)
	{
		name = nme;
		food = fd;
		language = lang;
	}

    public void eat() 
    {
        System.Console.WriteLine(name+" eats "+food);
    }

    public void speak() 
    {
        System.Console.WriteLine(name+" speaks "+language);
    }
}

class CreateHuman
{
	public static void Main(string[] args)
	{
        Human human1 = new Human("John", "Burger", "English","Air");

        human1.eat();
        human1.speak();

        human1.breathe();
	}
}


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from Air

So in the above example, we have an attribute named source and a method breathe() in the Parent class, LivingBeing.

C_Sharp

class LivingBeing
{
	string source;

	public LivingBeing()
	{

	}

	public LivingBeing(string sourceTemp)
	{
		source = sourceTemp;
	}

	public void breathe()
	{
		System.Console.WriteLine("Breathes oxygen from "+source);
	}
}

And we wanted to initialise the source attribute in the constructor of LivingBeing,


public LivingBeing(string sourceTemp)
{
	source = sourceTemp;
}

But as we know, the LivingBeing(string sourceTemp) constructor is only called at the time of Object creation.


And we won't be creating any objects of LivingBeing.


And this is where base comes to picture.


Let us see, how can we use the base to call the LivingBeing(string sourceTemp) constructor.


Then we have declared the Human class inheriting all the contents of the LivingBeing class.


class Human : LivingBeing
{
	string name;
	string food;
	string language;

	public Human()
	{

	}

	public Human(string nme, string fd, string lang, string sourceTemp) : base(sourceTemp)
	{
		name = nme;
		food = fd;
		language = lang;
	}

	public void eat()
	{
		System.Console.WriteLine(name+" eats "+food);
	}

	public void speak()
	{
		System.Console.WriteLine(name+" speaks "+language);
	}
}

Now, in the constructor of the Human class,


public Human(string nme, string fd, string lang, string sourceTemp) : base(sourceTemp)
{
	name = nme;
	food = fd;
	language = lang;
}

We have called the base(sourceTemp) constructor of the HumanBeing class using base.

C_Sharp

And the value of the attribute source gets set in the LivingBeing(string sourceTemp) constructor of the Parent class (i.e. HumanBeing).


public LivingBeing(string sourceTemp)
{
	source = sourceTemp;
}