Q:

C# program to demonstrate the concept of method hiding

0

C# program to demonstrate the concept of method hiding

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to demonstrate the concept of method Hiding is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the concept of method Hiding.
using System;

public class Sample
{
    public virtual void Method1()
    {
        Console.WriteLine("Sample: Method1() called");
    }
    public void Method2()
    {
        Console.WriteLine("Sample: Method2() called");
    }
}

public class Demo : Sample
{
    public override void Method1()
    {
        Console.WriteLine("Demo: Method1() called");
    }
    public new void Method2()
    {
        Console.WriteLine("Demo: Method2() called");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Demo Ob = new Demo();
        Ob.Method1();
        Ob.Method2();
    }
}

Output:

Demo: Method1() called
Demo: Method2() called
Press any key to continue . . .

Explanation:

In the above program, we created three classes SampleDemo, and Program. Here we inherited the Sample class into the Demo class and override the Method1() in the Demo class. Here we also defined method Method2() in both classes. In the derived class Demo, we gave a completely new definition and hide the inherited definition using the new keyword.

The Program class contains the Main() method, In the Main() method we created the object Ob of Demo class and then called Method1() and Method2().

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the example of anonymous... >>
<< C# program to demonstrate the index overloading...