How can I access a method defined with new keyword in a derived class

Sina Hoseinkhani

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)

mjwills

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

Polymorhism - how to access method of derived class?

From Dev

How can I access an object defined in another class?

From Dev

Can't access method defined in parent class

From Dev

Can't access method defined in parent class

From Dev

Derived class can't access protected method of base class

From Dev

Inherited method has no access to new implementation in derived class

From Dev

New keyword: why is the derived method not called?

From Dev

How to access Interface method implemented in the derived class from child class?

From Dev

How do I access this keyword In a different class

From Dev

How do I access this keyword In a different class

From Dev

How can a derived class invoke private method of base class?

From Dev

How do I access an inner class constructor from a derived class?

From Dev

How can I access injected Grails beans in an abstract class method?

From Dev

C++: How can I access a method of an inner class?

From Dev

Android:How can I access a value from a method of a class?

From Dev

Why i can't access protected property in the derived class

From Dev

How can I access the new javascript keyword from scala.js?

From Dev

Can I call a derived method from base class?

From Dev

How can I use `class` as a keyword argument?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

How can I friend a derived class function in the base class?

From Dev

How to access a method from another class without using extends keyword?

From Dev

How can a class call a class method that is defined on a singleton class?

From Dev

How can a class call a class method that is defined on a singleton class?

From Dev

Should I make 'count' as static in derived class, how to access it

From Dev

How to let a base method be defined only if the derived class does not define a method of the same name

Related Related

  1. 1

    How can I find out which method of a derived class is not implemented?

  2. 2

    How can I find out which method of a derived class is not implemented?

  3. 3

    Polymorhism - how to access method of derived class?

  4. 4

    How can I access an object defined in another class?

  5. 5

    Can't access method defined in parent class

  6. 6

    Can't access method defined in parent class

  7. 7

    Derived class can't access protected method of base class

  8. 8

    Inherited method has no access to new implementation in derived class

  9. 9

    New keyword: why is the derived method not called?

  10. 10

    How to access Interface method implemented in the derived class from child class?

  11. 11

    How do I access this keyword In a different class

  12. 12

    How do I access this keyword In a different class

  13. 13

    How can a derived class invoke private method of base class?

  14. 14

    How do I access an inner class constructor from a derived class?

  15. 15

    How can I access injected Grails beans in an abstract class method?

  16. 16

    C++: How can I access a method of an inner class?

  17. 17

    Android:How can I access a value from a method of a class?

  18. 18

    Why i can't access protected property in the derived class

  19. 19

    How can I access the new javascript keyword from scala.js?

  20. 20

    Can I call a derived method from base class?

  21. 21

    How can I use `class` as a keyword argument?

  22. 22

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  23. 23

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  24. 24

    How can I friend a derived class function in the base class?

  25. 25

    How to access a method from another class without using extends keyword?

  26. 26

    How can a class call a class method that is defined on a singleton class?

  27. 27

    How can a class call a class method that is defined on a singleton class?

  28. 28

    Should I make 'count' as static in derived class, how to access it

  29. 29

    How to let a base method be defined only if the derived class does not define a method of the same name

HotTag

Archive