Friday 2 May 2014

Inheritance in C#



Inheritance
A new class may inherit the members and functions of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
Inheritance implements the IS-A relationship. For example, Dog IS-A animal.

Practical Exercise to understand Inheritance in C#  

  1. Create a console application called SampleInheritance
  2. Right click SampleInheritance project and create three classes Animal, Dog and Lion
  3. Paste the following coding and execute the application to see the behavior of a call to a method in the sub class and base class
  4. Press Control + F5 to execute the program


Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleInheritance
{
    class Program
    {
        static void Main()
        {
            Dog sa = new Dog("dog");
            sa.Display();
            sa.AnimalEats("eat");       
            sa.Speak("speak");
            sa.AnimalLives("lives");
            Console.WriteLine("\n");

            Lion l = new Lion("lion");
            l.Display();
            l.AnimalEats("eat");
            l.Speak("speak");
            l.AnimalLives("lives");     
            Console.WriteLine("\n");

            Console.WriteLine("You are calling the ChangeName method from the base class");
            l.ChangeName("dog");
         
        }
    }
}


Animal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleInheritance
{
    public abstract class Animal // this is an abstract class
    {
    
        public string animalName; // Only accessible in base class


        public Animal(string animalNam) // constructor with one string argument
        {
           
            this.animalName = animalNam;
          
        }
        public virtual void Speak(string action) // Speak method is implemented in the abstract class , however since it has virtual key word it can be overidden in the derived class
        {
            if (action=="speak")
             Console.WriteLine("The animal speaks - Base class");
            else
                Console.WriteLine("Check your argument");

        }


        public virtual void Display() // Display method answers the question, which animal? , since it has the virtual key word it can be overidden in the derived class
        {
            Console.WriteLine("Name={0}", this.animalName+"- Base class");           
        }

        public void ChangeName(string newName)//This method changes the animal name or category of animal
        {
            this.animalName = newName;
            Console.WriteLine("Name={0}", this.animalName + "- Base class");    

        }

        public abstract  void AnimalEats(string action); // this is an abstract method with implementation in the derived class
        public abstract void AnimalLives(string action); // this is an abstract method with implementation in the derived class
     
       

    }


}

Dog.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleInheritance
{
    class Dog : Animal // Dog is a derived class which Inherits the base class Animal
    {
      

        public Dog( string animalName)            : base(  animalName)           // Call base-class constructor first
        {      
        }
  
        public override void Display()

        {
            base.Display();          // Call Display, inherited from base class
          
        }

        public override void Speak(string action) // Displays what is speak for a dog ?
        {
            if (action =="speak")


                Console.WriteLine("The dog barks - Derived class");
            else
                Console.WriteLine("Sorry data mismatch check if you are passing the right parameter");           
        }

        public override void AnimalEats(string action) // Displays what can a dog eat ?
        {
            if (action == "eat")
                Console.WriteLine("The dog eats cooked /processed/ or raw non-vegetarian items - Derived class");
            else
                Console.WriteLine("Sorry data mismatch check if you are passing the right parameter");

        }

        public override void AnimalLives(string action) // Displays where does a dog live ?
        {
            if (action == "lives")
                Console.WriteLine("The dog lives as a domestic animal - Derived class");
            else
                Console.WriteLine("Sorry data mismatch check if you are passing the right parameter");
        }
             
    }
   

}

Lion.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleInheritance
{
    class Lion : Animal // Lion is the sub class and Animal is the base class
    {
       

        public Lion( string animalName): base(animalName)           // Call base-class constructor first
        {
          
        }


        public override void Display() // This Display method overides the Display method in the Base class
        {
            Console.WriteLine("The animal is Lion - Derived class");
        }

        // If the following method is not commented it will overide the Speak method implemented in the base class

      /*  public override void Speak(string action)
          {
            if (action == "speak")

                Console.WriteLine("The lions roars - delived class");
            else
                Console.WriteLine("Sorry data mismatch check if you are passing the right parameter");

          }
        */

        public override void AnimalEats(string action) // This method displays what does a lion eat?
        {
            if(action=="eat")

                Console.WriteLine("The lion hunted or supplied meat - Derived class");
            else
                Console.WriteLine("Please check your argument");
        }

        public override void AnimalLives(string action) // This method displays where does the lion lives?
        {
            if (action== "lives" )
                Console.WriteLine("The lion lives in the forest - Derived class");
            else
                Console.WriteLine("Please check your argument");
        }
      
    }


}




No comments:

Post a Comment