Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Override with derived types?

As far as i know it is not possible to do the following in C# 2.0

public class Father {     public virtual Father SomePropertyName     {         get         {             return this;         }     } }  public class Child : Father {     public override Child SomePropertyName     {         get         {             return this;         }     } } 

I workaround the problem by creating the property in the derived class as "new", but of course that is not polymorphic.

public new Child SomePropertyName 

Is there any solution in 2.0? What about any features in 3.5 that address this matter?

like image 604
Luis Filipe Avatar asked Oct 01 '08 11:10

Luis Filipe


People also ask

How do you override a derived class method?

An override method is a new implementation of a member that is inherited from a base class. The overridden base method must be virtual, abstract, or override. Here the base class is inherited in the derived class and the method gfg() which has the same signature in both the classes, is overridden.

Which of the following Cannot be overridden in C#?

3. Which of the given modifiers can be used to prevent Method overriding? Explanation: When an instance method declaration includes the sealed modifier, the method is said to be sealed method. It means a derived class cannot override this method.

Can we override a property in C#?

In C# 8.0 and earlier, the return types of an override method and the overridden base method must be the same. You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method.

Which method can be redefined in derived class?

A derived class has the ability to redefine, or override, an inherited method, replacing the inherited method by one that is specifically designed for the derived class.


2 Answers

You can re-declare (new), but you can't re-declare and override at the same time (with the same name). One option is to use a protected method to hide the detail - this allows both polymorphism and hiding at the same time:

public class Father {     public Father SomePropertyName     {         get {             return SomePropertyImpl();         }     }     protected virtual Father SomePropertyImpl()     {         // base-class version     } }  public class Child : Father {     public new Child SomePropertyName     {         get         { // since we know our local SomePropertyImpl actually returns a Child             return (Child)SomePropertyImpl();         }     }     protected override Father SomePropertyImpl()     {         // do something different, might return a Child         // but typed as Father for the return     } } 
like image 78
Marc Gravell Avatar answered Sep 23 '22 06:09

Marc Gravell


This is not possible in any .NET language because of type-safety concerns. In type-safe languages, you must provide covariance for return values, and contravariance for parameters. Take this code:

class B {     S Get();     Set(S); } class D : B {     T Get();     Set(T); } 

For the Get methods, covariance means that T must either be S or a type derived from S. Otherwise, if you had a reference to an object of type D stored in a variable typed B, when you called B.Get() you wouldn't get an object representable as an S back -- breaking the type system.

For the Set methods, contravariance means that T must either be S or a type that S derives from. Otherwise, if you had a reference to an object of type D stored in a variable typed B, when you called B.Set(X), where X was of type S but not of type T, D::Set(T) would get an object of a type it did not expect.

In C#, there was a conscious decision to disallow changing the type when overloading properties, even when they have only one of the getter/setter pair, because it would otherwise have very inconsistent behavior ("You mean, I can change the type on the one with a getter, but not one with both a getter and setter? Why not?!?" -- Anonymous Alternate Universe Newbie).

like image 45
Alex Lyman Avatar answered Sep 23 '22 06:09

Alex Lyman