How does method inheritance work in c#: virtual override
This article will address the following
Overloading virtual and overriding hiding return types
The plot seems thicker than what the subject seem to indicate
satya - Wednesday, April 11, 2007 10:27:12 PM
there are three key words that controls this
new virtual override
satya - Wednesday, April 11, 2007 10:28:56 PM
java equivalence
use virtual on the base method and override on the derived
satya - Wednesday, April 11, 2007 10:29:18 PM
use abstract modifier if the method carries no body
use abstract modifier if the method carries no body
satya - Wednesday, April 11, 2007 10:30:39 PM
changing a base signature will result in incorrect java behavior
changing a base signature will result in incorrect java behavior
satya - Wednesday, April 11, 2007 10:32:16 PM
Indicate "virtual" explicitly in c#
Indicate "virtual" explicitly in c#
satya - Wednesday, April 11, 2007 10:33:03 PM
overloading is when a method has multiple signatures in the same class
overloading is when a method has multiple signatures in the same class
satya - Wednesday, April 11, 2007 10:46:31 PM
for override to work the methods should have the same signature
for override to work the methods should have the same signature
satya - Wednesday, April 11, 2007 10:46:56 PM
that may not be the case for hiding
that may not be the case for hiding
satya - Wednesday, April 11, 2007 10:49:12 PM
methods are overridden on a signature by signature basis
methods are overridden on a signature by signature basis
satya - Wednesday, April 11, 2007 10:50:14 PM
Here is a good document on java to cover these topics
satya - Wednesday, April 11, 2007 10:51:09 PM
an interesting note on overloading and overriding
If, for example, a class declares two public methods with the same name, and a subclass overrides one of them, the subclass still inherits the other method. In this respect, the Java programming language differs from C++.
satya - Wednesday, April 11, 2007 10:52:50 PM
overloading, overriding, and hiding in java
class Point { int x = 0, y = 0; void move(int dx, int dy) { x += dx; y += dy; } int color; } class RealPoint extends Point { float x = 0.0f, y = 0.0f; void move(int dx, int dy) { move((float)dx, (float)dy); } void move(float dx, float dy) { x += dx; y += dy; } }
satya - Wednesday, April 11, 2007 10:54:44 PM
a return type doesnt seem to be part of a method signature
a return type doesnt seem to be part of a method signature
satya - Wednesday, April 11, 2007 10:55:20 PM
it is a compile time error to override a method that differs only in its return type
it is a compile time error to override a method that differs only in its return type
satya - Friday, April 13, 2007 11:11:51 AM
signature does not include a return type
signature does not include a return type
satya - Friday, April 13, 2007 11:12:54 AM
You can not have two methods that differ only in their return types
You can not have two methods that differ only in their return types
satya - Friday, April 13, 2007 11:16:07 AM
method hiding in c#
A method introduced in a class or struct hides all non-method base class members with the same name, and all base class methods with the same signature (method name and parameter count, modifiers, and types)
satya - Friday, April 13, 2007 12:58:16 PM
different return types
You can not have two methods with different return types even the return types substitutable
satya - Friday, April 13, 2007 1:07:52 PM
In c# you can have different return types for the same method if they are derived
In c# you can have different return types for the same method if they are derived
satya - Friday, April 13, 2007 1:11:36 PM
example 1
public class TestBase { public void f1(String a) { Console.WriteLine("tbf1"); } public object f2(String a) { Console.WriteLine("tbf2"); return null; } public String f2(String a)// error: different return types { Console.WriteLine("tbf2s"); return null; } } public class TestDerived : TestBase { new public void f1(String a) { Console.WriteLine("tdf1"); } public String f2(String a) { Console.WriteLine("tdf2"); return null; } }
satya - Friday, April 13, 2007 1:42:02 PM
another example
public class TestBase { //parameter overloading in same class public void f_overloading() { Console.WriteLine("tbf1"); } public void f_overloading(String a) { Console.WriteLine("tbf1"); } public String f_overloading(int i) { return ""; } //ok: different signature //public String f_overloading(); //error: different return types with same params // '-' character is not allowed //different return type allowed in a derived class //it will be considered a new hidden method public object f_differentReturntype(String a) { return null; } //overload across derived public void f_overloading_derived() { } //virtual method public virtual void f_virtual() { } //error: different return types even of the same base type public virtual object f_virtual_different_returntypes() { return null; } } public class TestDerived : TestBase { //different return type allowed in a base class //warning: use new to fix it for hiding public String f_differentReturntype(String a) { return null; } //overload across derived //ok //same name different signatures public void f_overloading_derived(String a){} //virtual method public override void f_virtual() { } //error: different return types even of the same base type public override String f_virtual_different_returntypes() { return null; } }
satya - Friday, April 13, 2007 2:15:32 PM
Another note on return types for overloaded methods