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

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

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
}
}

Using operator sealed with the members of the class

I've found in the Troelsen's book, that operator sealed can be used on the members of the class to protect virtual methods from the override.
But if I don't want to override a virtual methods, what sense to make it virtual?
You might have a situation like this:
public class A
{
public virtual void MyMethod()
{
//...
}
}
public class B : A
{
public override void MyMethod()
{
//...
}
}
public class C : B
{
public override void MyMethod()
{
//...
}
}
But what if you want for the inheriting class C NOT to be able to override B's MyMethod, while still allowing B to override A's? Then you can do:
public class B : A
{
public sealed override void MyMethod()
{
//...
}
}
With this change made, you can no longer override the method in C.
In this context, consider the following example:
public class A
{
public virtual void SomeMethod() { }
}
public class B : A
{
public sealed override void SomeMethod() { }
}
public class C : B
{
public override void SomeMethod() { }
}
In this example, without the use of the sealed keyword on SomeMethod in class B, class C would be able to override it because it's original declaration was as virtual. The sealed keyword in this context generates a compiler error. See the MSDN for more information.

calling parent methods from overriden class in C#

I have the following code:
class ClassA
{
public virtual void DoSomething()
{
DoSomething(1); // when called from ClassB, it calls DoSomething from ClassA with an infinite recursion
}
public virtual void DoSomething(int someInt)
{
// do something
}
}
class ClassB : ClassA
{
public override void DoSomething()
{
DoSomething(1);
}
public override void DoSomething(int someInt)
{
base.DoSomething(someInt);
// do something
}
}
class Program
{
void someMethod()
{
ClassB instance = new ClassB();
instance.DoSomething(); // stack overflow caused by infinite recursion
}
}
My problem is that when I call ClassB.DoSomething(); and it calls base.DoSomething(someInt); I want the parent class which ClassB is derived to call ClassA's method instead of the overriden one.
Is there a way to do this in a clean way without copying/pasting repeated code?
You can modify the base class like so:
class ClassA
{
public virtual void DoSomething()
{
DoSomethingHelper(1); // when called from ClassB, it calls DoSomething from ClassA with an infinite recursion
}
public virtual void DoSomething(int someInt)
{
DoSomethingHelper(someInt);
}
private void DoSomethingHelper(int someInt)
{
// do something
}
}
By refactoring out the entire method into a private method you provide a means to call the current class' definition of the method while still providing a virtual method for the child class to access.
When you override the virtual method you can't call it without the base keyword, so you cannot cal if from that class. You can try using what Jon B said and use method shadowing instead of overriding.
class ClassA
{
public void DoSomething()
{
DoSomething(1);
}
public void DoSomething(int someInt)
{
Console.WriteLine("a");
}
}
class ClassB : ClassA
{
public new void DoSomething()
{
DoSomething(1);
}
public new void DoSomething(int someInt)
{
base.DoSomething();
}
}

C# how to override like this?

the code below is saying a() cannot override a() as wee.a() is not marked virtual, abstract or override. Is there a a way around this? I need to have code inside the super method, but still want to override it!
public abstract class wee
{
public void a()
{
}
}
public class dee : wee
{
public override void a()
{
}
public void b()
{
}
}
You need to mark wee.a virtual or abstract.
Virtual:
public abstract class wee
{
public virtual void a()
{
}
}
public class dee : wee
{
public override void a()
{
}
public void b()
{
}
}
Abstract:
public abstract class wee
{
public abstract void a();
}
public class dee : wee
{
public override void a()
{
}
public void b()
{
}
}
It will not override the method, there is no way to do this without marking the base as such. If you don't want to modify the base as others have suggested, instead you can hide the base method like this:
public class dee : wee
{
public new void a()
{
}
public void b()
{
}
}
Mark wee.a() as virtual -- it allows you to provide a base implementation but gives the option to override that behavior in subclasses if needed.
// wee
public virtual void a() { // do stuff for base implementation }
// dee
public override void a() { // override behavior implemenation }

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

Categories