method-hiding

How method hiding works in C#? (Part Two)

谁说胖子不能爱 提交于 2019-12-04 23:59:08
The following program prints A:C(A,B) B:C(A,B) (as it should) public interface I { string A(); } public class C : I { public string A() { return "A"; } public string B() { return "B"; } } public class A { public virtual void Print(C c) { Console.WriteLine("A:C(" + c.A() + "," + c.B() + ")"); } } public class B : A { public new void Print(C c) { Console.WriteLine("B:C(" + c.A() + "," + c.B() + ")"); } public void Print(I i) { Console.WriteLine("B:I(" + i.A() + ")"); } } class Program { public static void Main(string[] args) { A a = new A(); B b = new B(); C c = new C(); a.Print(c); b.Print(c);

java non-static to static method — hiding or overriding

拟墨画扇 提交于 2019-12-03 17:05:25
is re-defining a non-static method in a subclass with the same everything but as static overriding or hiding it ? http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html says hiding. but when i declare the superclass method as final, i get an override error. superclass declaration is final static void display() { ... } subclass: void display() { ... } gives override error. Is re-defining a non-static method in a subclass with the same everything but as static overriding or hiding it? It's neither, because doing so triggers a compilation error , rendering your program invalid. class A

Hide a base class method in a generic derived class

孤人 提交于 2019-12-01 21:05:20
I have a base class like this: class FooBase { public bool Do(int p) { /* Return stuff. */ } } And a child class like this: class Foo<T> : FooBase { private Dictionary<T, int> Dictionary; public bool Do(T p) { int param; if (!Dictionary.TryGetValue(p, out param)) return false; return base.Do(param); } } If the user creates a Foo<string> object called "fooString", then he can call both fooString.Do(5) and fooString.Do("test") but if he creates a Foo<int> object called "fooInt", he can only call the Do method of the derived class. I prefer the second no matter what the T is. The Do methods in

Hide a base class method in a generic derived class

我只是一个虾纸丫 提交于 2019-12-01 20:35:47
问题 I have a base class like this: class FooBase { public bool Do(int p) { /* Return stuff. */ } } And a child class like this: class Foo<T> : FooBase { private Dictionary<T, int> Dictionary; public bool Do(T p) { int param; if (!Dictionary.TryGetValue(p, out param)) return false; return base.Do(param); } } If the user creates a Foo<string> object called "fooString", then he can call both fooString.Do(5) and fooString.Do("test") but if he creates a Foo<int> object called "fooInt", he can only

Prevent derived classes from hiding non virtual functions from base

我与影子孤独终老i 提交于 2019-12-01 18:58:25
问题 Consider that I have classes A & B such that class A { public: void Fun(); }; class B : public A { .... }; Is there any way that I as a designer of class A can enforce that derived class B and other classes which derive from A, are prevented(get a some kind of error) from hiding the non virtual function Fun()? 回答1: If you want the non- virtual member function to always be accessible in some way, then simply wrap it in a namespace scope free function: namespace revealed { void foo( A& o ) { o

C# and method hiding

老子叫甜甜 提交于 2019-11-30 15:37:56
Per MSDN, the "new" keyword when used for method hiding only suppresses a warning. http://msdn.microsoft.com/en-us/library/435f1dw2.aspx Do I really need this "new" keyword to perform method hiding? If I have a child class that has the same method name but doesn't explicitly state that it is overriding, isn't that the same thing essentially, I just get a warning? Please tell me if my understanding is correct. Thanks You get method hiding whether or not you specify "new", but it's not the same as overriding. Here's an example where they're different: using System; class Base { public virtual

What scenarios does it make sense to use “method hiding”? [duplicate]

戏子无情 提交于 2019-11-30 03:10:13
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: method hiding in c# with a valid example .why is it implemented in the framework.? what is the Real world advantage.? I am wondering how method hiding could be useful in c# ? If you dive into the concept ,you can find that method hiding is something confusing and drives us to spaghetti code style.There are some reasons for that : 1- If you need a fresh method in your Derived class you can define a new method

C# and method hiding

冷暖自知 提交于 2019-11-29 21:59:43
问题 Per MSDN, the "new" keyword when used for method hiding only suppresses a warning. http://msdn.microsoft.com/en-us/library/435f1dw2.aspx Do I really need this "new" keyword to perform method hiding? If I have a child class that has the same method name but doesn't explicitly state that it is overriding, isn't that the same thing essentially, I just get a warning? Please tell me if my understanding is correct. Thanks 回答1: You get method hiding whether or not you specify "new", but it's not the

What are the differences between overriding virtual functions and hiding non-virtual functions?

纵然是瞬间 提交于 2019-11-27 00:52:10
Given the following code fragment, what are the differences in the function calls? What is function hiding? What is function overriding? How do they relate to function overloads? What is the difference between the two? I couldn't find a good description of these in one place, so I'm asking here so I can consolidate the information. class Parent { public: void doA() { cout << "doA in Parent" << endl; } virtual void doB() { cout << "doB in Parent" << endl; } }; class Child : public Parent { public: void doA() { cout << "doA in Child" << endl; } void doB() { cout << "doB in Child" << endl; } };

What are the differences between overriding virtual functions and hiding non-virtual functions?

主宰稳场 提交于 2019-11-26 09:30:58
问题 Given the following code fragment, what are the differences in the function calls? What is function hiding? What is function overriding? How do they relate to function overloads? What is the difference between the two? I couldn\'t find a good description of these in one place, so I\'m asking here so I can consolidate the information. class Parent { public: void doA() { cout << \"doA in Parent\" << endl; } virtual void doB() { cout << \"doB in Parent\" << endl; } }; class Child : public Parent