Automatically call base method - c#

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()

Related

Abstract and virtual functions

I am trying to implement two level inheritance. Currently, there is an abstract class and an inherited class :
public abstract class A
{
public abstract void func();
}
public class B : A
{
public override void func()
{
.......
}
}
I would like to create two specialized instances of class B but I want those functions to be exposed by class A. I am going for,
public abstract class A
{
public abstract void func();
}
public class B : A
{
public virtual void func();
}
public class C : B
{
public override void func()
{
........
}
}
public class D : B
{
public override void func()
{
........
}
}
This implementation is wrong but that is my intent. How will I implement this ?
You can use interface instead of abstract class A like this:
public interface A
{
void func();
}
public abstract class B: A
{
public abstract void func();
}
public class C : B
{
public override void func()
{
throw new NotImplementedException();
}
}
public class D : B
{
public override void func()
{
throw new NotImplementedException();
}
}
May be it helps you.
You cannot have virtual method without implementation, so instead you should make class B abstract, which should make compiler happy:
public abstract class B : A
{
}
Alternative approach is to add empty method body for function func:
public class B : A
{
public virtual void func()
{
// function has empty method body
// it does not do anything, but you can override functionality in derived classes
}
}

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();
}
}

How to override a method inherited from a base class, which in turn implemented it from an interface?

I have a class B that inherits from class A, which in turn inherits from interface I. This interface exposes a method M which, of course, is implemented by A, but I would like to override it in B. Moreover, I would like to call A.M from B.M. How do I do that?
EDIT: The answers made me feel kind of stupid, especially since I know what virtual means and, in fact, I have tried it:
class A
{
virtual void I.M() // fails
I never even considered not implementing the interface explicitly.
Thank you all.
Well, either you need to make the method virtual in A, or you need to reimplement the interface in B, which gets messy. Here's the simpler version:
using System;
public interface IFoo
{
void M();
}
public class A : IFoo
{
public virtual void M()
{
Console.WriteLine("A.M");
}
}
public class B : A
{
public override void M()
{
base.M();
Console.WriteLine("B.M");
}
}
class Test
{
static void Main()
{
IFoo foo = new B();
foo.M();
}
}
... and here's the version which reimplements IFoo, hiding A.M() instead of overriding it:
using System;
public interface IFoo
{
void M();
}
public class A : IFoo
{
public void M()
{
Console.WriteLine("A.M");
}
}
public class B : A, IFoo
{
public new void M()
{
base.M();
Console.WriteLine("B.M");
}
}
class Test
{
static void Main()
{
IFoo foo = new B();
foo.M();
}
}
Note that if you then had:
A a = (A) foo;
a.M();
it would only call A.M(), not B.M().
You can use the New keyword to hide the base M as well, heres a short linqpad program
The diffrence between virtual and new
http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-s-the-difference-between-code-override-code-and-code-new-code.aspx
void Main()
{
var x = new B();
x.M();
}
public interface I
{
void M();
}
public class A : I
{
public void M()
{
"A.M".Dump();
}
}
public class B : A
{
public new void M()
{
"B.M".Dump();
base.M();
}
}
Results:
B.M
A.M
Make M virtual in A
interface ISome
{
void M();
}
class B : ISome
{
public virtual M()
{
}
}
class A : B
{
public void override M()
{
base.M();
}
}
When you implement method M in class A, implement it as "virtual", then when you want to override it in class B use the "override" keyword, which will allow you to do just that.
interface I {
void M();
}
class A : I {
virtual void M() {
}
}
class B : A {
override void M() {
//Do stuff;
base.M();
}
}

How to access functions from abstract class without making them static?

I want to create a class that can only be inherited, for that i know it should be made abstract. But now the problem is that i want to use functions of that class without making them static. How can i do that.
public abstract Class A
{
A()
{}
public void display()
{}
}
public Class B:A
{
base.A() // this is accessible
this.display() // this is not accessible if i dont make this function static above
}
Your example will not compile, you could consider something like this:
using System;
public abstract class A
{
protected A()
{
Console.WriteLine("Constructor A() called");
}
public void Display()
{
Console.WriteLine("A.Display() called");
}
}
public class B:A
{
public void UseDisplay()
{
Display();
}
}
public class Program
{
static void Main()
{
B b = new B();
b.UseDisplay();
Console.ReadLine();
}
}
Output:
Constructor A() called
A.Display() called
Note: Creating a new B() implicitly calls A(); I had to make the constructor of A protected to prevent this error:
"'A.A()' is inaccessible due to its protection level"
That's not true. You don't have to make Display() static; you can call it freely from the subclass. On the other hand, you can't call the constructor like that.
Maybe it's just an error in the example, but the real issue with the code you have is that you can't put method calls in the middle of your class definition.
Try this:
public abstract class A
{
public void Display(){}
}
public class B:A
{
public void SomethingThatCallsDisplay()
{
Display();
}
}
Here's how you can do this..
public abstract class A
{
public virtual void display() { }
}
public class B : A
{
public override void display()
{
base.display();
}
public void someothermethod()
{
this.display();
}
}

Can I prevent an inherited virtual method from being overridden in subclasses?

I have some classes layed out like this
class A
{
public virtual void Render()
{
}
}
class B : A
{
public override void Render()
{
// Prepare the object for rendering
SpecialRender();
// Do some cleanup
}
protected virtual void SpecialRender()
{
}
}
class C : B
{
protected override void SpecialRender()
{
// Do some cool stuff
}
}
Is it possible to prevent the C class from overriding the Render method, without breaking the following code?
A obj = new C();
obj.Render(); // calls B.Render -> c.SpecialRender
You can seal individual methods to prevent them from being overridable:
public sealed override void Render()
{
// Prepare the object for rendering
SpecialRender();
// Do some cleanup
}
Yes, you can use the sealed keyword in the B class's implementation of Render:
class B : A
{
public sealed override void Render()
{
// Prepare the object for rendering
SpecialRender();
// Do some cleanup
}
protected virtual void SpecialRender()
{
}
}
In B, do
protected override sealed void Render() { ... }
try sealed
class B : A
{
protected sealed override void SpecialRender()
{
// do stuff
}
}
class C : B
protected override void SpecialRender()
{
// not valid
}
}
Of course, I think C can get around it by being new.
An other (better ?) way is probablby using the new keyword to prevent a particular virtual method from being overiden:
class A
{
public virtual void Render()
{
}
}
class B : A
{
public override void Render()
{
// Prepare the object for rendering
SpecialRender();
// Do some cleanup
}
protected virtual void SpecialRender()
{
}
}
class B2 : B
{
public new void Render()
{
}
}
class C : B2
{
protected override void SpecialRender()
{
}
//public override void Render() // compiler error
//{
//}
}
yes. If you mark a method as Sealed then it can not be overriden in a derived class.

Categories