In method Hiding also parent class methods can be redefine under child classes even if they were not declared as Virtual by using 'new' keyword.
Example :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class methodhiding
{
public void demo()
{
Console.WriteLine(" this is method");
}
}
class methodhiding1 : methodhiding
{
public new void demo()
{
Console.WriteLine(" this is method hiding");
}
static void Main()
{
methodhiding1 m = new methodhiding1();
methodhiding m1 = m;
m1.demo();
Console.WriteLine();
m.demo();
Console.ReadLine();
}
}
}
Example :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class methodhiding
{
public void demo()
{
Console.WriteLine(" this is method");
}
}
class methodhiding1 : methodhiding
{
public new void demo()
{
Console.WriteLine(" this is method hiding");
}
static void Main()
{
methodhiding1 m = new methodhiding1();
methodhiding m1 = m;
m1.demo();
Console.WriteLine();
m.demo();
Console.ReadLine();
}
}
}
Output :
this is method
this is method hiding
this is method
this is method hiding
No comments:
Post a Comment