This question already has answers here:
How do I call a derived class method from the base class?
(2 answers)
Closed 4 years ago.
I have this array in program
ClassA[] array=new ClassA[20];
array[0]=new ClassB();
array[1]=new ClassA();
This is the class file
public class ClassA
{
public void method()
{
Console.WriteLine("1");
}
}
public class ClassB : ClassA
{
public void method()
{
Console.WriteLine("2");
}
}
It writes 1, in both cases, but i want in first case to write 2, (to call method of ClassB).
How to do that?
You have to use override keyword in C# when you want to override method in child class.
public class ClassA
{
public virtual void method()
{
Console.WriteLine("1");
}
}
public class ClassB : ClassA
{
public override void method()
{
Console.WriteLine("2");
}
}
Regards.
Related
This question already has answers here:
Difference between new and override
(14 answers)
what is "public new virtual void Method()" mean?
(4 answers)
Closed 2 years ago.
In this example it uses polymorphysm via virtual, override keywords
abstract class A
{
public virtual string Print() { return "A"; }
}
class B : A
{
public override string Print() { return "B"; }
}
class C : B
{
public virtual new string Print() { return "C"; }
}
class D : C
{
public override string Print() { return "D"; }
}
class Program
{
static void Main(string[] args)
{
A a = new D();
Console.WriteLine(a.Print());
}
}
Console prints B. Why B, not D? Thank you for answers
Simple, because the last time you override the Print function of class A is during the declaration of class B
class B : A
{
public override string Print() { return "B"; }
}
By declaring public virtual new, you are hiding the underlying implementation of Print. So during the D declaration you are overriding the new implementation, declared at class C
Now A, has no knowledge whatsoever of what you've done, once you've hidden the function on B
That's the problem with methods that hide methods of their parent classes. It is not polymorphic. Since the variable is of type A the resolution of method Print() only knows about A.Print() and B.Print().
C.Print() and D.Print() are not overrides of A.Print(), because of the declation of C.Print() with new.
This question already has answers here:
Why does calling a method in my derived class call the base class method?
(16 answers)
Closed 3 years ago.
I have a base and sub class such as:
class BaseClass
{
public void MethodA()
{
MethodB();
}
public void MethodB()
{
Debug.Log("BaseClass MethodB");
}
}
class SubClass : BaseClass
{
public new void MethodB() // <- without `new` keyword there's a warning on this line
{
Debug.Log("SubClass MethodB");
base.MethodB();
}
}
When the MethodA of the BaseClass instance is called it calls MethodB but only of the BaseClass, and not of the SubClass first. e.g.
var subclass = new SubClass();
subclass.MethodA(); // Does not log "SubClass MethodB" first. Only logs "BaseClass MethodB"
How do you make sure the parent methods call the subclass methods?
Note, without the new keyword on the MethodB line, Visual Studio gives a warning: 'SubClass.MethodB()' hides inherited member 'BaseClass.MethodB()'. Use the new keyword if hiding was intended.
To get Subclass.MethodB to be called from your base class you need to use the virtual and override keywords. For example:
class BaseClass
{
public void MethodA() // invoking this will correctly log "SubClass MethodB" followed by "BaseClass MethodB"
{
MethodB();
}
public virtual void MethodB()
{
Debug.Log("BaseClass MethodB");
}
}
class SubClass : BaseClass
{
public override void MethodB()
{
Debug.Log("SubClass MethodB");
base.MethodB();
}
}
This question already has answers here:
Force base method call
(14 answers)
Closed 4 years ago.
I would like to override a method, but i still want the parent method to be called when i override. When i try to use :base() to call parent method, it says unexpected token.
public class A
{
public virtual void DoStuff()
{
//some code
}
}
public class B : A
{
public override void DoStuff() : base()
{
//some other code
}
}
In java i would be :
super.DoStuff()
You have the wrong syntax, you can call the base method using the base keyword anywhere in the body of the method. The :base() syntax is only for constructors.
public class A
{
public virtual void DoStuff()
{
//some code
}
}
public class B : A
{
public override void DoStuff()
{
base.DoStuff();
}
}
This question already has answers here:
How to use class name as parameter in C#
(4 answers)
Closed 5 years ago.
Is it possible to pass ClassA as argument without using typeof or initializing it?
class ClassA {
}
class ClassB {
public void Function([something here] clazz) {
......
}
}
class ClassC {
public void main() {
ClassB asdf = new ClassB();
asdf.Function(ClassA); // pass like that, not typeof() or something else
}
}
Yes, you can pass the type as a generic parameter.
class ClassA {
}
class ClassB {
public void Function<T>() {
......
}
class ClassC {
public void main() {
ClassB asdf = new ClassB();
asdf.Function<ClassA>(); // magic
}
}
This sort of thing is very common with IoC containers and autofactories:
var o = container.Resolve<SomeClassName>();
This question already has answers here:
Difference between this and base
(9 answers)
Closed 9 years ago.
If you are writing code in a class that inherits from a base class and you want to call a protected or public method on that base class, is it best (right or wrong or otherwise) to call base.MyProtectedMethod() or this.MyProtectedMethod() (in c#)? What would the difference be? Both seem to work.
For example:
public class MyBase()
{
....
protected void DoStuff()
{
// some stuff
}
}
public class MyChildClass() : MyBase
{
public MyNewMethod()
{
// do some work
this.DoStuff();
base.DoStuff();
}
}
Does this just to the same thing twice in MyNewMethod?
This does exactly the same thing in MyNewMethod.
I would only recommend using base. when it is actually required, ie: when you need to explicitly calling the base class version of a method from within an overridden method.
Do you want to explicitly call up the parent class? Then use base.
If you don't want to, use this.
This shows a great example on using base.
Just to illustrate Reed and Kevin's answers:
namespace ConsoleApplication1
{
class A
{
public virtual void Speak()
{
Hello();
}
virtual protected void Hello()
{
Console.WriteLine("Hello from A");
}
}
class B : A
{
public override void Speak()
{
base.Hello(); //Hello from A
this.Hello(); //Hello from B
}
override protected void Hello()
{
Console.WriteLine("Hello from B");
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Speak();
}
}
}