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.
Related
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.
I am new in c#.confuse with this question.
I Have override ToString() with override and new keyword.both are give me same output.then what is the difference between both.
here is my Example
class A
{
public new string ToString()
{
return "With New Keyword";
}
}
class B
{
public override string ToString()
{
return "With Override Keyword";
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Console.Read();
}
}
Output
With New Keyword
With Override Keyword
I know its a silly question.
please anyone help me to give me difference between both methods.
I am not asking about difference between new and override key word.I want to know that difference between both method.In concept of Override Object methods.
It makes a difference when you do this:
object a = new A(); // notice the type of the variable here!
object b = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
a.ToString() will not call your implementation of ToString and will instead call object.ToString, which returns the fully qualified type name of the object. b.ToString() will call your implementation.
What you did in B is called overriding. What you did in A is called hiding. Hiding loses its effect when the compile time type of a variable is not that type anymore. Your implementation of ToString will only be called when the compile time type is A.
Learn more here.
You can easily google this.
From MSDN
In C#, a method in a derived class can have the same name as a method
in the base class. You can specify how the methods interact by using
the new and override keywords. The override modifier extends the base
class method, and the new modifier hides it. The difference is
illustrated in the examples in this topic.
In a console application, declare the following two classes, BaseClass
and DerivedClass. DerivedClass inherits from BaseClass.
In class B you are overriding ToString() method that is Object.ToString() because you have not inherited A. Now consider below example,
class A
{
public string ReturnString()
{
return "ClassA::Method";
}
}
class B : A
{
public newstring ReturnString()
{
return "ClassB::Method";
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Console.Read();
}
}
Here you are hiding the method in class B as you have inherited Class A. So you will get the output as ClassA::Method for both method calls.
Now consider below example
class A
{
public virtual string ReturnString()
{
return "ClassA::Method";
}
}
class B : A
{
public override string ReturnString()
{
return "ClassB::Method";
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Console.Read();
}
}
Here we have overridden the Class A's method in Class B. This means ClassB method is like extension or you can say it has different meaning than ClassA. You will get output like below
ClassA::Method
ClassB::Method
This question already has answers here:
Overload resolution and virtual methods
(5 answers)
Closed 5 years ago.
Case 1:
public class BaseClass
{
public virtual void Print(int i)
{
Console.WriteLine("BaseClass Print(int)");
}
}
public class DerivedClass : BaseClass
{
public override void Print(int i)
{
Console.WriteLine("DerivedClass Print(int)");
}
public void Print(object obj)
{
Console.WriteLine("DerivedClass Print(object)");
}
}
static void Main(string[] args)
{
DerivedClass objDerivedClass = new DerivedClass();
int i = 10;
objDerivedClass.Print(i);
}
Output is DerivedClass Print(object).
Case 2:
public class SomeClass
{
public void Print(int i)
{
Console.WriteLine("DerivedClass Print(int)");
}
public void Print(object obj)
{
Console.WriteLine("DerivedClass Print(object)");
}
}
static void Main(string[] args)
{
SomeClass objSomeClass = new SomeClass();
int i = 10;
objSomeClass.Print(i);
}
Output is DerivedClass Print(int).
After calling objDerivedClass.Print(i); method, the output is DerivedClass Print(object). I don't understand why the method Print(object obj) is being called instead of Print(int i).
If DerivedClass does not inherit BaseClass class then output is DerivedClass Print(int).
Please explain.......
This is how overload resolution works with inheritance:
The function that includes the override modifier is excluded from the set of candidates.
Since the function with the object parameter can be used, then the function that is declared in the base is removed from the set of candidates.
The winner is the function with the object parameter.
According to Eric Lippert (from Microsoft):
This is by design and for a good reason. This design helps prevent the Brittle Base Class problem. C# was designed to make it easier and safer to write "versioned" components, and this rule is a big part of that.
When no inheritance is used, both functions are candidates, and the one that is more specific is used. The winner is the function with the int parameter.
This question already has answers here:
new keyword in method signature
(9 answers)
Closed 9 years ago.
I have seen methods that are declared like this:
public void new SortItems()
What does this actually do? I know that the new keyword is used to invoke constructors, but I have also seen it on method definitions like the example above.
When use this way, it's a modifier. It's used to hide an inherited member rather than to override it. This is useful if the base method is sealed. Here's a quick example to demonstrate the difference between overriding and hiding inherited members:
public class Foo
{
public virtual void OverriddenMethod() { Console.WriteLine("foo"); }
public void HiddenMethod() { Console.WriteLine("foo"); }
}
public class Bar : Foo
{
public override void OverriddenMethod() { Console.WriteLine("bar"); }
public new void HiddenMethod() { Console.WriteLine("bar"); }
}
void Main()
{
Bar bar = new Bar();
Foo foo = bar;
bar.OverriddenMethod(); // "bar"
bar.HiddenMethod(); // "bar"
foo.OverriddenMethod(); // "bar"
foo.HiddenMethod(); // "foo"
}
Further Reading
new Modifier (C# Reference)
It should be like this:
public new void SortItems(){
//...
}
This new keyword is used to shadow the base member (method, property, ...) which has the same name (for property, event...) and same signature (for method), in this case it is the method SortItems. It's different from the new in creating new instance. No matter using new to shadow the conflicted member in base class or not, to access the base member you have to use the keyword base to access it in the derived class.
When used in a method signature, it means that the implementation details are different for the class defining them. The problem with this approach is that it is not used polymorphically so:
class Thing
{
void DoSomething()
{
Console.WriteLine("Thing");
}
}
class Other : Thing
{
new void DoSomething()
{
Console.WriteLine("Other");
}
}
var thing = new Thing();
thing.DoSomething(); \\ prints Thing
var other = new Other();
other.DoSomething(); \\ prints Other
((Thing)other).DoSomething(); \\ prints Thing
It is the opposite of override. Say you have:
public class A
{
public virtual void f() { Console.WriteLine( "A" ); }
}
public class B : A
{
public override void f() { Console.WriteLine( "B" ); }
}
public class C : A
{
public new void f() { Console.WriteLine( "C" ); }
}
And then in main:
A b = new B();
A c = new C();
b.f();
c.f();
(c as C).f();
This would print:
B
A
C
It will only call the new method when the type is that of the defining class.
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();
}
}
}