Empty abstract method override -- good or bad design?
-
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
-
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
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
-
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
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. -
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.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
-
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
: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.