Overriding, Overridden Function - c#

here's what I am trying to do
class A
{
virtual void foo();
}
class B : A
{
virtual override void foo();
}
class C : B
{
override void foo();
}
so what I want to see when calling C.foo() is
A.foo(), B.foo(), C.foo()
but I dont think virtual override can be used in the same function definition. How would I go around this?
Thanks
-Mike

Overridden functions are automatically virtual, unless explicitly declared sealed.
Note that calling C.Foo() will not call B.Foo() or A.Foo() unless C.Foo manually calls base.Foo().

code should work like this:
public class A
{
public virtual void foo() {}
}
public class B : A
{
public override void foo() {}
}
class C : B
{
public override void foo() {}
}
so long as foo is accessible. no need for 'virtual' in B

Is this what you're looking for?
class A
{
public virtual void foo()
{
MessageBox.Show("A");
}
}
class B : A
{
public override void foo()
{
MessageBox.Show("B");
base.foo();
}
}
class C : B
{
public override void foo()
{
MessageBox.Show("C");
base.foo();
}
}
C c = new C();
c.foo();

Related

how do i refer to the derived type from the base class?

is there a keyword for the question "?" mark below or a way to achieve the same effect without using templates?
abstract class A
{
public abstract void Attach(? x);
}
class B : A
{
public override void Attach(B b) {}
}
class C : A
{
public override void Attach(C c) {}
}
so that:
var b1 = new B();
var b2 = new B();
var c = new C();
b1.Attach(b2);
b1.Attach(c); // should not compile
EDIT:
with templates i mean type parameters such as Attach<T>(T x, T y) // if we ignore that the example takes 1 argument
Annoyingly, no. The closest you can get is:
abstract class A<T> where T : A<T>
{
public abstract void Attach(T x);
}
class B : A<B>
{
public override void Attach(B b) { }
}
class C : A<C>
{
public override void Attach(C c) { }
}
This doesn't however stop someone from writing:
class D : A<B>
{
...
}
If you want to avoid this, you need a runtime check for this.GetType() == typeof(T) or similar in A's constructor.
You can make A Generic like so:
abstract class A<T> where T : A<T>
{
public abstract void Attach(T x);
}
class B : A<B>
{
public override void Attach(B b) {}
}
class C : A<C>
{
public override void Attach(C c) {}
}
Than the following does not comile
b1.Attach(c); // should not compile

How can I make a private child function visible to the base class?

Assume that I have the following classes with a generic interface:
public class MyClass_1 : IGenericInterface
{
public void foo()
{
bar();
}
void bar()
{
...
}
}
public class MyClass_2 : IGenericInterface
{
public void foo()
{
bar();
}
void bar()
{
...
}
}
public interface IGenericInterface
{
void foo();
}
They both have the public "foo" function and it's implementation is the same as well. So I tend to move "foo" into an abstract, shouldn't I?
Now, the problem is, that they call a private function ("bar") which is a class specific implementation. How can I make this function "bar" kind of visible to the possibly generic implementation of "foo"?
E.g.
public abstract class MyGenericClass: IGenericInterface
{
public foo()
{
bar();
}
}
The goal then would be that the specific class inherit from the generic class:
public class MyClass_1 : GenericClass
{
private void bar()
{
...
}
}
public class MyClass_2 : GenericClass
{
private void bar
{
...
}
}
Define bar as abstract:
public abstract class MyGenericClass: IGenericInterface
{
protected abstract void bar();
public foo()
{
bar();
}
}
Then bar can be implemented differently in each subclass, but the base class can be sure it exists:
public class MyClass_1 : GenericClass
{
protected override void bar
{
// ...
}
}
Make it an abstract protected method in your base class:
public abstract class MyGenericClass: IGenericInterface
{
public foo()
{
bar();
}
protected abstract void bar();
}
Protected means that it will be visible in all derived classes, abstract means that your derived classes have to implement it (if they are not abstract themselves).

How to call a method from a parent class's base class

Can I call a method from a grandparent class, and if so, how?
I'm trying to do something like this:
class A {
void foo() {
// Do something...
}
}
class B : A {
override void foo() {
// Do something else...
}
}
class C : B {
override void foo() {
// Call A's foo method
// Then do something else
}
}
One approach is to use explicit interface implementation in class A:
This allows you to call A's implementation - both from code within C and from outside C (by casting to IBob first).
using System;
namespace ConsoleApp4
{
interface IBob
{
void foo();
}
class A : IBob
{
void IBob.foo()
{
Console.WriteLine("A");
}
public virtual void foo()
{
((IBob)this).foo();
}
}
class B : A
{
public override void foo()
{
Console.WriteLine("B");
}
}
class C : B
{
public override void foo()
{
Console.WriteLine("C");
// Writes B
base.foo();
// Writes A
((IBob)this).foo();
}
}
public class Program
{
static void Main(string[] args)
{
var sally = new C();
sally.foo(); // A B C
IBob sally2 = sally;
sally2.foo(); // A
Console.ReadLine();
}
}
}

How to access the first base class in C#?

I'm having a base class defined in my app
Something like,
using System;
public class BaseClass
{
public virtual void Method2() {Console.WriteLine("base2");}
public virtual void Method1() {Console.WriteLine("base1");}
}
public class Derived1 : BaseClass
{
public override void Method2() {Console.WriteLine("derived1-2");}
public override void Method1() {Console.WriteLine("derived1-1");}
}
public class Derived2 : Derived1
{
public override void Method1() {Console.WriteLine("derived2-2");}
}
public class Program
{
public static void Main()
{
var obj = new Derived2();
((BaseClass)obj).Method2();
Console.WriteLine("Hello World");
}
}
Say I need to access the Method2() of my very first BaseClass. Even after typecasting it to ((BaseClass)obj).Method2(), I'm still getting derived1-2 while I'm expecting base2'.
How do I do that ?
I completely agree with the comments: it sounds like the behaviour that you require would need you to re-think your classes - possibly inheritance is not the best solution in this case. It depends what you're trying to do; you could obviously just do this:
var obj = new BaseClass();
((BaseClass)obj).Method2();
Console.WriteLine("Hello World");
My guess would be that what you actually want here is some form of injection; for example:
public class BaseClass
{
private readonly IFunctionality1 functionality1;
public virtual void Method2() { Console.WriteLine("base2"); }
public virtual void Method1() { this.functionality1.Method1(); }
public BaseClass(IFunctionality1 functionality1)
{
this.functionality1 = functionality1;
}
}
public class Derived1 : BaseClass
{
public Derived1(IFunctionality1 functionality1) : base (functionality1)
{
}
}
In this case, you might find that inheritance isn't even required anymore.

would `as` enable polymorphism? would pass a inherited class to a method which takes a base class enable polymorphism?

Fist of all, I will use virtual and override
for example, base class A has method A.do(), inherited class B has B.do() which overrides A's.
if I call (B as A).do(), which do() would it execute?
or, if there is a method void mymethod(A a) {a.do()}, now I call it by B b; mymethod(b), would it execute b.do()?
The most top override method always will be called, i.e. b.Do() or (b as A).Do() or ((A)b).Do() will call B.Do().
I don't know a way how to call a base method from child class if child class overrides it.
public class A
{
public virtual void Do() { Console.Write("a"); }
public void Do2() { Console.Write("a2"); }
}
public class B : A
{
public override void Do() { Console.Write("b"); }
public new void Do2() { Console.Write("b2"); }
}
class Program
{
static void Main(string[] args)
{
B b = new B();
A a = b;
b.Do(); //b
( b as A ).Do(); //b
a.Do(); //b
( (A)b ).Do(); //b
( b as A ).Do2(); //a2
( (A)b ).Do2(); //a2
( b ).Do2(); //b2
}
}
Output:
b b b b
a2 a2 b2
It entirely depends on whether the do() method was declared virtual or not. If it is not virtual then A.do() is called. If it is virtual then B.do() is called. It is the virtual keyword that enables polymorphism and allows calling a method independent of the type of the reference.
There is no mechanism in C# that allows directly calling a virtual A.do() method from a B object reference. The only exception is using base.do() inside an instance method of class B.
public class A
{
public A() { }
public void Do() { Console.Write("A"); }
}
public class B : A
{
public B() { }
public void Do() { Console.Write("B"); }
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Do(); //<-- outputs B
(b as A).Do(); //<-- outputs A
}
}
compiler warns for hiding not overriding:
Warning 1 'ConsoleApplication5.B.Do()' hides inherited member
'ConsoleApplication5.A.Do()'. Use the new keyword if hiding was
intended. c:\Program.cs 18 21 ConsoleApplication5
that is since you are not overriding anything, but simply hiding the method from A.
however
public class A
{
public A() { }
public virtual void Do() { Console.Write("A"); }
}
public class B : A
{
public B() { }
public override void Do() { Console.Write("B"); }
}
calls B twice, when method is overridden.

Categories

Resources