Hiding of member function does not work - c#

I guess I have I miss-conception. I try to override the behavior of a class in a sub-class by replacing a public function. In the program below I expect "B" to be written to the console, but the program prints "A". Where do I think wrong? And how can I achieve it. (In my real case I cannot change class A).
class Program
{
class A { public void F() { Console.WriteLine("A"); } }
class B : A { public new void F() { Console.WriteLine("B"); } }
static void Main(string[] args)
{
A x;
x = new B();
x.F();
Console.ReadLine();
}
}

The desired behaviour can be obtained by using a virtual method and overriding it at follows; note the override keyword.
class Program
{
class A { public virtual void F() { Console.WriteLine("A"); } }
class B : A { public override void F() { Console.WriteLine("B"); } }
static void Main(string[] args)
{
A x;
x = new B();
x.F();
Console.ReadLine();
}
}
If you happen to have a Java background, note that this type of inheritance is the default behaviour in Java, but in C# it has to be explicitly declared.

Related

Could C# delegate point to another class/object's method?

Here, I wish to gain a delegate like function pointer, to point to a class method that's in another class (named Inner), and then pass it to a static function, like below:
public class Inner
{
public int M = 3;
public void F() { Console.WriteLine("f"); }
public void G() { Console.WriteLine("g"); }
}
class Program
{
public static void Caller(Action a)
{
a();
}
static void Main(string[] args)
{
var i = new Inner();
var f = i.F;
var g = i.G;
f();//error
g();//error
Program.Caller(f);
Console.WriteLine("Hello World!");
}
}
I'm from c/c++, and in c/c++, function pointer like this is very straight forward, but this C# code fail to compile. I googled and found almost all delegate explanations talks about delegate that points to class method inside itself.
My question is, how to fix the code to make it work?
Coding Seb's answer highlight's the cause of the issue, but doesn't really explain why.
It is because i.F is a method group, and not a specific method pointer. For example, imagine Inner was defined as so:
public class Inner
{
public void F() { Console.WriteLine("f"); }
public void F(string name) { Console.WriteLine(name); }
}
Which method would i.F refer to? F() or F(string)?
For that reason, you need to define the variable type explicitly, or cast the pointer:
Action f = i.F;
Or:
var f = (Action)i.F;
You can not set methods group in implicit variables in C# so if you just change 2 var in Action it's working
public class Inner
{
public int M = 3;
public void F() { Console.WriteLine("f"); }
public void G() { Console.WriteLine("g"); }
}
class Program
{
public static void Caller(Action a)
{
a();
}
static void Main(string[] args)
{
var i = new Inner();
Action f = i.F;
Action g = i.G;
f();
g();
Program.Caller(f);
Console.WriteLine("Hello World!");
}
}

Polymorphism in C#. Explain output

I expected output C in this program. But real result is A.
Please, explain why program prints A.
class A
{
public virtual void say()
{
Console.WriteLine ("A");
}
}
class B : A
{
public new virtual void say()
{
Console.WriteLine ("B");
}
}
class C : B
{
public override void say()
{
Console.WriteLine ("C");
}
}
class MainClass
{
public static void Main (string[] args)
{
A a = new C ();
a.say();
}
}
It's because you created new virtual method say() in the class B.
This new method hides original method A.say(), so in the class C you overriden this new method B.say() but not the A.say().
And since you declared your object as A
A a = new C ();
the old A.say() method is called.
You're not overriding the say method in Class B, which is the sub class of Class C.
public new virtual void say()
In the above line, you're hiding the say method. Look at the new modifier here on MSDN

How to extend method in C# subclass compared to Objective-C

I come from Objective-C. I'm learning C# now. How to achieve the C# equivalent of the following Objective-C call ?
- (void)doSomething
{
[super doSomething]
// do more stuff
}
Use the base keyword:
public void doSomething(){
base.doSomething();
}
public class A
{
public virtual void doSomething() { Console.WriteLine("Class A"); }
}
class B : A
{
public override void doSomething()
{
base.doSomething();
Console.WriteLine("Class Y");
}
}
static void Main()
{
A b = new B();
b.doSomething();
Console.ReadKey();
}

Advantage of calling a method using base pointer

I have implemented a method in the base class as following:
class A
{
protected void f1()
{
}
}
class A1 : A
{
public void f2()
{
//Simple Calling
f1();
//Calling using base pointer
base.f1();
}
}
What is the difference between the calling simply and calling using a base pointer ? What are the advantages of either of the ways?
In your example there is no difference. However, consider situation when f1 is virtual and it has another implementation in A1 class:
class A
{
protected virtual void f1()
{
Console.WriteLine("A");
}
}
class A1 : A
{
public void f2()
{
//Simple Calling - prints `A1`
f1();
//Calling using base pointer - prints `A`
base.f1();
}
protected override void f1()
{
Console.WriteLine("A1");
}
}
f1() is different than base.f1() then. The same situation appears when you use new keyword to hide base implementation within derived class:
protected new void f1()
{
Console.WriteLine("A1");
}
The difference between this.f1() (or simply f1()) and base.f1() becomes relevant when you override a virtual method:
class A
{
public virtual void F()
{
Console.WriteLine("A");
}
}
class B : A
{
public override void F()
{
Console.WriteLine("B");
}
void Test()
{
F(); // Prints "B"
this.F(); // Prints "B"
base.F(); // Prints "A"
}
}
It's only useful if you have overloaded/shadowed a method defined in the base class.
class A1 : A
{
public void f2()
{
//Simple Calling
f1();
//Calling using base pointer
base.f1();
}
protected new void f1()
{
// I won't be called
}
}
Also useful when you want to extend the functionality of a base method, but don't want to replicate it:
class A
{
public virtual void F()
{
Console.WriteLine("A");
}
}
class B : A
{
public override void F()
{
base.F();
Console.WriteLine("B");
}
void Test()
{
F(); // Prints "A B"
}
}
In this case, none. But imagine this:
class A
{
public virtual void F()
{
}
}
class B : A
{
public override void F()
{
Console.WriteLine("B");
}
public void F2()
{
F(); /*output: B*/
base.F() /*no output*/
}
}
That's where base starts to come in useful.
The base keyword is used to refer to the base class when chaining constructors or when you want to access a member (method, property, anything) in the base class that has been overridden or hidden in the current class.
For example,
class A {
protected virtual void Foo() {
Console.WriteLine("I'm A");
}
}
class B : A {
protected override void Foo() {
Console.WriteLine("I'm B");
}
public void Bar() {
Foo();
base.Foo();
}
}
With these definitions,
new B().Bar();
would output
I'm B
I'm A
Reference
if you override f1, base its needed to differentiate between them.
class A
{
protected virtual void f1() { }
}
class A1 : A
{
protected override void f1() { }
public void f2()
{
//Simple Calling
f1(); <--- this will call to overrided f1 in this class
//Calling using base pointer
base.f1();
}
}

Automatically call base method

I need to call automatically base class method when calling overriden one (like constructors call base). For example:
class A
{
public void Fun()
{
Console.Write("Class A!");
}
}
class B : A
{
public void Fun()
{
Console.Write("Class B!");
}
}
I want to see on the screen
Class A! Class B!
when executing next code:
B b = new B();
b.Fun();
Could anyone tell me please what need to change in example code or how better to write to get required result? Thanks.
If you don't want to call it explicitly and therefore ensure A.Fun() is called in the derived class, you could use something called the template method pattern:
class A
{
public void Fun()
{
Console.Write("Class A!");
FunProtected();
}
protected virtual void FunProtected()
{
}
}
class B : A
{
protected override void FunProtected()
{
Console.Write("Class B!");
}
}
This would give you:
new A().Fun() -> "Class A!"
new B().Fun() -> "Class A! Class B!"
If you want such behavior you will need to change platform/language. .NET doesn't automatically call the base method. You need to call it explicitly. Also your method needs to be virtual or you will be hiding it in the derived class:
class A
{
public virtual void Fun()
{
Console.Write("Class A!");
}
}
class B : A
{
public override void Fun()
{
// call the base method which will print Class A!
base.Fun();
Console.Write("Class B!");
}
}
Now when you do this:
B b = new B();
b.Fun();
you will get the required result.
Can still be so:
interface IFun
{
void Fun();
}
abstract class A : IFun
{
void IFun.Fun()
{
Console.Write("Class A!");
Fun();
}
protected abstract void Fun();
}
class B : A
{
protected override void Fun()
{
Console.Write("Class B!");
}
}
IFun b = new B();
b.Fun();
This works if object reference is IFun.
Just for reference, you could use a wrapper pattern that puts an A inside a B. Here is a crude example!
interface IFunClass {
void Fun();
}
class A : IFunClass {
public void IFunClass.Fun() {
Console.Write("Class A!");
}
}
class B : IFunClass {
public B(IFunClass f) {
this.m_SomethingFun = f;
}
public void IFunClass.Fun() {
this.m_SomethingFun.Fun();
Console.Write("Class B!");
}
private IFunClass m_SomethingFun;
}
A a = new A();
B b = new B(a);
b.Fun() // will call a.Fun() first inside b.Fun()

Categories