Reputation: 3361
As a Java programmer (beginner) introducing myself to C#, I have found you can re-abstract an already implemented method like this (code from this answer)
public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}
public abstract class E : D
{
public abstract override void DoWork(int i);
}
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}
I am aware that this doesn't make the original implementation in class D completely unavailable, only unavailable to subclasses of D that subclass it through subclassing E (the class that re-abstracts the method), and as such shouldn't cause any actual problems if class D was in production.
Still, I find myself wondering, is this not modifying class D's contract, which does NOT specify subclasses MUST override DoWork(int i)
? Is this not contrary to the open/closed principle?
Please note I haven't had any actual or even theoretical code broken by this, and keep in mind I'm only starting with C# so I might be missing something here.
Upvotes: 1
Views: 147
Reputation: 628
Code practices remain largely constant across different programming languages, such as C# and Java.
I do not see any issue with doing this assuming you are still using good object oriented design. Just make sure you are not accidentally messing up the original functionality in the first concrete class (D
in this case). That's the only issue I would watch out for.
Regarding your switch to C#, I think you will quickly fall in love with .NET and wonder how you ever went without it!
Best of luck with the new language. Hope this helped :)
Upvotes: 2