C# Tips of Inheritance and Instance of Class

Look at given below steps:

  • If method in the base and derived class are same and not made virtual in the base class and not being override in the derived then following case will occur :
    1. using System;  
    2.   
    3. public class Program  
    4. {  
    5.    public static void Main()  
    6.    {  
    7.       Base objBase = new Base();  
    8.       objBase.Message(); // I amd Base  
    9.   
    10.       Derived objDerived = new Derived();  
    11.       objDerived.Message(); // I am Derived  
    12.   
    13.       // Now, set the reference of derived class to base class.  
    14.   
    15.       objBase = new Derived();  
    16.       objBase.Message(); // I am Base  
    17.   
    18.    }  
    19. }  
    20.   
    21.   
    22. public class Base  
    23. {  
    24.    public void Message()  
    25.    {  
    26.       Console.WriteLine("I am Base");  
    27.    }  
    28. }  
    29.   
    30. public class Derived : Base  
    31. {  
    32.    public void Message()  
    33.    {  
    34.       Console.WriteLine("I am Derived");  
    35.    }  
    36. }  
  • Output:

    I am Base
    I am Derived
    I am Base

    Executing Code URL: .NET Fiddle.

  • If method in the base and derived class are same and made virtual in the base class and being override in the derived then following case will occur:
    1. using System;   
    2. public class Program  
    3. {  
    4.    public static void Main()  
    5.    {  
    6.       Base objBase = new Base();  
    7.       objBase.Message(); // I amd Base  
    8.   
    9.       Derived objDerived = new Derived();  
    10.       objDerived.Message(); // I am Derived  
    11.   
    12.       // Now, set the reference of derived class to base class.  
    13.   
    14.       objBase = new Derived();  
    15.       objBase.Message(); // I am Derived  
    16.   
    17.    }  
    18. }  
    19.   
    20. public class Base  
    21. {  
    22.    public virtual void Message()  
    23.    {  
    24.       Console.WriteLine("I am Base");  
    25.    }  
    26. }  
    27.   
    28. public class Derived : Base  
    29. {  
    30.    public override void Message()  
    31.    {  
    32.       Console.WriteLine("I am Derived");  
    33.    }  
    34. }  
    Executing Code URL: DotNetFiddle.

  • If method in the base and derived class are same and not made virtual in the base class and not being override in the derived. Also, new keyword is being applied with the derived class method then following case happens:
    1. using System;   
    2. public class Program  
    3. {  
    4.    public static void Main()  
    5.    {  
    6.       Base objBase = new Base();  
    7.       objBase.Message(); // I amd Base  
    8.   
    9.       Derived objDerived = new Derived();  
    10.       objDerived.Message(); // I am Derived  
    11.   
    12.       // Now, set the reference of derived class to base class.  
    13.   
    14.       objBase = new Derived();  
    15.       objBase.Message(); // I am Base  
    16.   
    17.    }  
    18. }  
    19.   
    20.   
    21. public class Base  
    22. {  
    23.    public void Message()  
    24.    {  
    25.       Console.WriteLine("I am Base");  
    26.    }  
    27. }  
    28.   
    29. public class Derived : Base  
    30. {  
    31.    public new void Message()  
    32.    {  
    33.       Console.WriteLine("I am Derived");  
    34.    }  
    35. }  
    Output:

    I am Base
    I am Derived
    I am Base

    Executing Code URL: DotNetFiddle.
Ebook Download
View all
Learn
View all