method-hiding

How to use method hiding (new) with generic constrained class

一个人想着一个人 提交于 2020-12-26 08:39:19
问题 I have a container class that has a generic parameter which is constrained to some base class. The type supplied to the generic is a sub of the base class constraint. The sub class uses method hiding (new) to change the behavior of a method from the base class (no, I can't make it virtual as it is not my code). My problem is that the 'new' methods do not get called, the compiler seems to consider the supplied type to be the base class, not the sub, as if I had upcast it to the base. Clearly I

“this” keyword type when called on an object of derived class from base class

纵然是瞬间 提交于 2020-01-24 11:02:53
问题 If I have something like this: class Base { public void Write() { if (this is Derived) { this.Name();//calls Name Method of Base class i.e. prints Base ((Derived)this).Name();//calls Derived Method i.e prints Derived } else { this.Name(); } } public void Name() { return "Base"; } } class Derived : Base { public new void Name() { return "Derived"; } } and use the following code to call it, Derived v= new Derived(); v.Write(); // prints Base then the Name method of base class gets called. but

can overriding of a method be prevented by downcasting to a superclass?

痴心易碎 提交于 2019-12-25 02:22:10
问题 I'm trying to understand whether the answer to the following question is the same in all major OOP languages; and if not, then how do those languages differ. Suppose I have class A that defines methods act and jump ; method act calls method jump . A 's subclass B overrides method jump (i.e., the appropriate syntax is used to ensure that whenever jump is called, the implementation in class B is used). I have object b of class B . I want it to behave exactly as if it was of class A . In other

How to hide functions within classes in c#?

时光怂恿深爱的人放手 提交于 2019-12-13 04:39:29
问题 I need to hide few methods inside a class based on the parameter that is passed to the constructor in c#. How would I do this? Thanks in advance! More Info: I was part of this GUI development where they had a API with access to registers in a hardware. Now they are releasing a newer hardware so I need to support both old and new one which has a newer API(Mostly duplicate of old one with some old removed and some new registers added). Moreover, I need to keep this only one class called "API"

Are type aliases used as type of function parameter part of the function signature?

ε祈祈猫儿з 提交于 2019-12-10 16:09:22
问题 Consider an example: #include <iostream> #include <vector> template <class, class T> using alias = T; template <template <class...> class> struct tt_wrapper{}; template <class...> struct t_wrapper{}; struct A { template <template <class...> class TT, class T> void foo(alias<tt_wrapper<TT>, T>) { std::cout << "A::foo invoked" << std::endl; } }; struct B: A { using A::foo; template <class U, class T> void foo(alias<t_wrapper<U>, T>) { std::cout << "B::foo invoked" << std::endl; } }; int main()

What is the difference between method hiding and shadowing in C#?

馋奶兔 提交于 2019-12-07 10:52:48
问题 What is the difference between method hiding and shadowing in C#? Are they same or different? Can we call them as polymorphism (compile time or run time)? 回答1: What is the difference between method hiding and shadowing in C#? Shadowing is another commonly used term for hiding. The C# specification only uses "hiding" but either is acceptable. You call out just "method hiding" but there are forms of hiding other than method hiding. For example: namespace N { class D {} class C { class N { class

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

大城市里の小女人 提交于 2019-12-06 18:21:43
问题 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

Why does Java restrict the access modifier of a hiding method [closed]

余生颓废 提交于 2019-12-06 03:30:58
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . When hiding a static field, there's no restriction on what access level does the field have in subclass, it can be even non-static and of other data type. On the other side, when hiding a static method, the static method from subclass that hides the static method from

Inherit method with unrelated return types

吃可爱长大的小学妹 提交于 2019-12-05 13:21:36
I have the following code snippet public class Test { static interface I1 { I1 m(); } static interface I2 { I2 m(); } static interface I12 extends I1,I2 { I12 m(); } public static void main(String [] args) throws Exception { } } When I try to compile it I got error. Test.java:12: types Test.I2 and Test.I1 are incompatible; both define m(), but with unrelated return types. How to avoid this? Matthias As discussed in Java - Method name collision in interface implementation you can't do this. As a workaround, you can create an adapter class. Edwin Dalorzo There is only one case in which this

java non-static to static method — hiding or overriding

↘锁芯ラ 提交于 2019-12-05 06:20:58
问题 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. 回答1: Is re-defining a non-static method in a subclass with the same everything but as static overriding or