Inheritance
-
Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks
-
Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks
Can you show the minimum amount of code required to demonstrate the situation?
-
Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks
The least you can do is show the code you are working with, and make some effort. Look up the words 'new and 'override in the context of .NET inheritance, think about them, try using them, then, come back here with specific questions and code.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
-
Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks
That's a confusing sentence, but make sure the methods in the base class are marked as virtual.
Regards, Rob Philpott.
-
Hi, I have a class A that inherits another class B and calls a method in Class B. That method in turn calls another method which is overridable in Class B. Now, I want the inner method to be created in the calling Class A. But I noticed that the control comes to the first method in Class B and then goes to the inner method in Class B itself. Is it possible to bypass that inner method and call my overrided method instead? Thanks
Yes - that is exactly what inheritance and method overriding is all about!
public class A : B
{
public void DoIt()
{
Console.WriteLine("A:DoIt");
InB();
}
public override void Inner()
{
base.Inner();
Console.WriteLine("A:Inner");
}
}
public class B
{
public void InB()
{
Console.WriteLine("B:InB");
Inner();
}
public virtual void Inner()
{
Console.WriteLine("B:Innner");
}
}Then when you create an instance:
A a = new A(); a.DoIt();
You get:
A:DoIt
B:InB
B:Innner
A:InnerBecause B.Inner is marked as
virtual
it can be overridden in derived classed - as it is in A - and the system will look for the most "appropriate" version to call. Becausea
is declared as an instance of the A class, it calls the A version of the method. If you declared a as a B instead but assigned it an A (which is perfectly fine as every A is a "superset" of B):B b = new A(); b.Inner();
You can't call the DoIt method (because B doesn't contain a definition for DoIt), but you will still get:
B:Innner
A:InnerBecuase the value inside the variable
b
is still an instance of the A class so the system calls the "latest" version.Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Yes - that is exactly what inheritance and method overriding is all about!
public class A : B
{
public void DoIt()
{
Console.WriteLine("A:DoIt");
InB();
}
public override void Inner()
{
base.Inner();
Console.WriteLine("A:Inner");
}
}
public class B
{
public void InB()
{
Console.WriteLine("B:InB");
Inner();
}
public virtual void Inner()
{
Console.WriteLine("B:Innner");
}
}Then when you create an instance:
A a = new A(); a.DoIt();
You get:
A:DoIt
B:InB
B:Innner
A:InnerBecause B.Inner is marked as
virtual
it can be overridden in derived classed - as it is in A - and the system will look for the most "appropriate" version to call. Becausea
is declared as an instance of the A class, it calls the A version of the method. If you declared a as a B instead but assigned it an A (which is perfectly fine as every A is a "superset" of B):B b = new A(); b.Inner();
You can't call the DoIt method (because B doesn't contain a definition for DoIt), but you will still get:
B:Innner
A:InnerBecuase the value inside the variable
b
is still an instance of the A class so the system calls the "latest" version.Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...