I have the following classes:
class SearchEngine
{
public virtual void Search()
{
Console.WriteLine("Base Class");
}
}
class Google: SearchEngine
{
public override void Search()
{
Console.WriteLine("Google Class");
}
}
class Yahoo: SearchEngine
{
public new void Search()
{
Console.WriteLine("new method in Yahoo Class");
}
}
And I call them this way:
SearchEngine s1 = new SearchEngine();
SearchEngine s2 = new Google();
SearchEngine s3 = new Yahoo();
s1.Search();
s2.Search();
s3.Search();
And I get the following results:
Base Class
Google Class
Base Class
My question is how to access the Search()
method from the Yahoo
class? (And yes I know this is a bad idea to use the new
keyword like this and I know this not overriding a class)
To do this you need to change:
SearchEngine s3 = new Yahoo();
to:
var s3 = new Yahoo();
or:
Yahoo s3 = new Yahoo();
Your new
method is not 'accessible' from the base class. That is the whole point of new
when specified on a method.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments