Inheritance Question
-
I have a class of UserControls all of which need to implement the same method. I can achieve this with an abstract method public abstract void foo(); However this has a disadvantage: (A) There is some commonality in the code that it would make sense to extract and put into the base class. I know that I can achieve this by //Base public virtual void foo ( BaseCode(); ) //Derived public override void foo ( SomeCode(); base.foo(); ) But then that gives me 2 disadvantages (B) It doesn't force a derived class to do it's own implementation of foo (C) if the derived class doesn't explicitly call base.foo(); my common code doesn't get called. Is there some other modifier or pattern I can use to achieve A, B, and C? Or. Is there a good reason why I should not be trying to achieve this?
-
I have a class of UserControls all of which need to implement the same method. I can achieve this with an abstract method public abstract void foo(); However this has a disadvantage: (A) There is some commonality in the code that it would make sense to extract and put into the base class. I know that I can achieve this by //Base public virtual void foo ( BaseCode(); ) //Derived public override void foo ( SomeCode(); base.foo(); ) But then that gives me 2 disadvantages (B) It doesn't force a derived class to do it's own implementation of foo (C) if the derived class doesn't explicitly call base.foo(); my common code doesn't get called. Is there some other modifier or pattern I can use to achieve A, B, and C? Or. Is there a good reason why I should not be trying to achieve this?
You can try splitting it up into three methods. One method has your base code. The other method is abstract and must be overridden by subclasses. And the last method calls the base code method first, then the abstract method.
-
I have a class of UserControls all of which need to implement the same method. I can achieve this with an abstract method public abstract void foo(); However this has a disadvantage: (A) There is some commonality in the code that it would make sense to extract and put into the base class. I know that I can achieve this by //Base public virtual void foo ( BaseCode(); ) //Derived public override void foo ( SomeCode(); base.foo(); ) But then that gives me 2 disadvantages (B) It doesn't force a derived class to do it's own implementation of foo (C) if the derived class doesn't explicitly call base.foo(); my common code doesn't get called. Is there some other modifier or pattern I can use to achieve A, B, and C? Or. Is there a good reason why I should not be trying to achieve this?
A, B: If the method that you put in the base class doesn't fully implement what the method should do, you shouldn't implement it at all. Make it abstract, and put the code in a protected method that the derived classes can use to implement the abstract method. C: You can't force anything about how an overridden method is implemented. If you want some code to always be called from the method, let the derived class implement another method instead, that is used by that method: Base class:
public void Foo() { FooTask(); SomeOtherCode(); } protected abstract void FooTask();
Derived class:protected override void FooTask() { SpecificImplementation(); }
--- b { font-weight: normal; }