1

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.

Community
  • 1
  • 1
Blueriver
  • 3,212
  • 3
  • 16
  • 33
  • 2
    Broken by this? The entirety of WinForms, as well as WPF, is based on this. – Blindy Aug 29 '15 at 02:45
  • 2
    An overriding method can still call the method it is overriding. For example `public override void DoWork(int i) { return base.DoWork(i); }`. This allows a developer to extend the functionality of that method to an extent, so I'd say it is still adhering to the open/closed principle since you're not modifying the original directly. – Eraph Aug 29 '15 at 02:51
  • 4
    `as a java programmer` - you will find a lot of things in C# that might be outside your current frame of reference, which is pretty limited, regardless of how many years of java programming you might have. Keep your mind open, language features are there to help you, despite what java and its status quo philosophy that rejects any kind of evolution and progress might have led you to believe. – Federico Berasategui Aug 29 '15 at 03:13
  • @HighCore Nice touch the the end there, made me smile :) – Owen James Aug 29 '15 at 03:24
  • @HighCore thank you, that's a big tip I think. I will try to be as open-minded as possible! @Eraph as far as I understand abstract methods I can't do `public abstract override void DoWork(int i) { return base.DoWork(i); }` in class E, which would be the only one who could call `base.DoWork(i);` and access to class D's implementation. Furthermore, I can't do `public override void DoWork(int i) { return base.DoWork(i); }` in class F because class E (the base of class F) declares it as abstract. – Blueriver Aug 29 '15 at 06:24

1 Answers1

2

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 :)

Owen James
  • 628
  • 7
  • 16
  • 2
    Have an upvote, though I'm not 100% sure I agree with your first statement. Coding practices remain the same, except for those that are common place in java and are made completely irrelevant by C#'s language features (such as that horrible "anonymous inner class" thing in java which is a poor workaround for the lack of real events, etc) – Federico Berasategui Aug 29 '15 at 03:32
  • 1
    I see. So I have to be careful, which I didn't need to be in Java! Just kidding. Thank you for the answer! So far I like what I see, and I'm already thinking about going further than required by this semester at university and actually learning C#. Hence the question :) – Blueriver Aug 29 '15 at 06:36
  • @Blueriver Best of luck with your courses! C# is my personal favorite but I recommend you use whatever you feel most comfortable with :) – Owen James Aug 29 '15 at 06:41