Access the Base Class Method with Derived Class Objects

Let us say we have to call the base class method from the derived class objects.

So we need to Typecast the derived class object to the base class and then call the method of base class

Lets see the code
  1. using System;    
  2. public class BaseClass    
  3. {    
  4.     public void Method1()    
  5.     {    
  6.         Console.WriteLine("Base class Method....");    
  7.     }    
  8. }    
  9. public class DerivedClass: BaseClass    
  10. {    
  11.     public new void Method1()    
  12.     {    
  13.         Console.WriteLine("Derived class Method....");    
  14.     }    
  15. }    
  16. class Program    
  17. {    
  18.     static void Main(string[] args)    
  19.     {    
  20.         DerivedClass derivedObj = new DerivedClass();    
  21.         BaseClass obj2 = (BaseClass) derivedObj; // cast to base class    
  22.         obj2.Method1(); // invokes Baseclass method    
  23.     }    
  24. }    
Hope you got the concept .Thanks for reading.