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. Design and Architecture
  4. Empty abstract method override -- good or bad design?

Empty abstract method override -- good or bad design?

Scheduled Pinned Locked Moved Design and Architecture
designoopquestion
5 Posts 3 Posters 1 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.
  • B Offline
    B Offline
    brontofuzik
    wrote on last edited by
    #1

    Hi, the situation is as follows: There are 2 classes - C1 and C2 - both inherited from an abstract class A. The reason is that instances of C1 and C2 are to be stored in an inhomogeneous collection, and a common base class (A) is used to access their functionality (textbook polymorphism). The abstract class A defines an abstract method M. This is meaningfully overriden in C1, but has no meaningful override in C2. Common sense would suggest to remove it from the abstract class if either of its derived classes are unable to override it meaningfully. However, the logic of the situation requires M to be defined in the abstract class A. My questions are these: 1. Is it a sign of good or bad design to write empty abstract method overrides? 2. Is it a sign of good or bad design to write abstract method overrides that only throw an exception informing the user of an incorrect usage? 3. How common are the empty abstract method overrides in real-world programming practice? Your answers are greatly appreciated! Lukas

    L M 2 Replies Last reply
    0
    • B brontofuzik

      Hi, the situation is as follows: There are 2 classes - C1 and C2 - both inherited from an abstract class A. The reason is that instances of C1 and C2 are to be stored in an inhomogeneous collection, and a common base class (A) is used to access their functionality (textbook polymorphism). The abstract class A defines an abstract method M. This is meaningfully overriden in C1, but has no meaningful override in C2. Common sense would suggest to remove it from the abstract class if either of its derived classes are unable to override it meaningfully. However, the logic of the situation requires M to be defined in the abstract class A. My questions are these: 1. Is it a sign of good or bad design to write empty abstract method overrides? 2. Is it a sign of good or bad design to write abstract method overrides that only throw an exception informing the user of an incorrect usage? 3. How common are the empty abstract method overrides in real-world programming practice? Your answers are greatly appreciated! Lukas

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, my 2 eurocents: 0. there is not only good or bad design, there is a continuum from very good to very bad 1. IMO overriding a method by an empty one because the functionality does not make sense is wrong. 2. throwing an exception in an override is OK 3. empty abstract overrides are only correct if that particular class needs a no-operation there; however I have never seen one. 4. FWIW, I would try and remove from the base class all functionality that is not meaningful for some of its decendents. If cats and dogs are mammals that eat and walk, and cats purr but dogs don't, then purr would not be a mammals method. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      • B brontofuzik

        Hi, the situation is as follows: There are 2 classes - C1 and C2 - both inherited from an abstract class A. The reason is that instances of C1 and C2 are to be stored in an inhomogeneous collection, and a common base class (A) is used to access their functionality (textbook polymorphism). The abstract class A defines an abstract method M. This is meaningfully overriden in C1, but has no meaningful override in C2. Common sense would suggest to remove it from the abstract class if either of its derived classes are unable to override it meaningfully. However, the logic of the situation requires M to be defined in the abstract class A. My questions are these: 1. Is it a sign of good or bad design to write empty abstract method overrides? 2. Is it a sign of good or bad design to write abstract method overrides that only throw an exception informing the user of an incorrect usage? 3. How common are the empty abstract method overrides in real-world programming practice? Your answers are greatly appreciated! Lukas

        M Offline
        M Offline
        Mark Churchill
        wrote on last edited by
        #3

        It seems like you have a list of things, and you want to go through the list and perform an action which is only relevant to some things in it. In this case you could consider exposing an interface, and then when you iterate thru the list you can use if(item is INeedToBeNotifiedAboutEclipses) ((INeed..)item).YoItsALittleDarkOutHere();

        Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
        Entanglar: .Net game engine featuring automatic networking and powerful HLSL gfx binding.

        L 1 Reply Last reply
        0
        • M Mark Churchill

          It seems like you have a list of things, and you want to go through the list and perform an action which is only relevant to some things in it. In this case you could consider exposing an interface, and then when you iterate thru the list you can use if(item is INeedToBeNotifiedAboutEclipses) ((INeed..)item).YoItsALittleDarkOutHere();

          Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
          Entanglar: .Net game engine featuring automatic networking and powerful HLSL gfx binding.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Mark Churchill wrote:

          INeedToBeNotifiedAboutEclipses

          No problem.[^] :cool:

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            Mark Churchill wrote:

            INeedToBeNotifiedAboutEclipses

            No problem.[^] :cool:

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            M Offline
            M Offline
            Mark Churchill
            wrote on last edited by
            #5

            :D

            Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
            Entanglar: .Net game engine featuring automatic networking and powerful HLSL gfx binding.

            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