Access to the base class Function A using an object of class B in C #

Access to the base class Function using a derived class B in C # Is there any way that I can access the function (Sum) of an object of class A by class B and get the output 10.?

Class A { public int Sum(int i) { return i+3; } } Class B:A { public int Sum(int i) { return i+4; } } B objectB=new B(); int result=objectB.Sum(7); output:11 
+6
source share
1 answer

Declare a variable A instead of B , continuing to use constructor B

 A objectB = new B(); int result=objectB.Sum(7); 

This will use method A This is true only because the method is obscured and not overridden.

You will also receive a compiler warning for your Sum method in B , and you can define it as public new int Sum(int i) to signal that the hide is intended.

+8
source

Source: https://habr.com/ru/post/956516/


All Articles