Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Inheritance Question

Inheritance Question

Scheduled Pinned Locked Moved C#
questionregexoop
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MartinSmith
    wrote on last edited by
    #1

    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?

    D G 2 Replies Last reply
    0
    • M MartinSmith

      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?

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      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.


      Logifusion[^]

      1 Reply Last reply
      0
      • M MartinSmith

        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?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups