"I'm not back." - Bill Harding, Twister
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Classes with inherited or declared abstract methods must be declared abstract and cannot be instantiated. In your first example, if you required Mother to be non-abstract (sometimes called concrete), then using an exception is your only option.Originally posted by Tian Zhang:
though on second thoughts this must be the case. in a nonabstract class methods cannot be abstract, as this would cause problems if the class is instantiated and the "abstract" method being called.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
When I've used this technique, it is because the abstract superclass needs to call the abstract method but cannot provide a reasonable default behavior for the method. If the superclass doesn't declare it, it cannot call it and let the subclass provide an implementation.Originally posted by Ilja Preuss:
Why do you need to declare the method in the superclass? Can't you just have the appropriate subclasses implement an addtional interface?
Originally posted by David Harkness:
When I've used this technique, it is because the abstract superclass needs to call the abstract method but cannot provide a reasonable default behavior for the method. If the superclass doesn't declare it, it cannot call it and let the subclass provide an implementation.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Tian Zhang:
Ilja Preuss do you mean the following solution:
though on second thoughts i do not require the super to be concrete, in which case i would guess that Keith Lynn's solution would be more appropriate. (i am guessing that the main advantage of extracting the interface is that it allows the "Person" class to be concrete).
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Of course, that's precisely the point. When the method is optional, I use the UnsupportedOperationException route instead of making it abstract.Originally posted by Ilja Preuss:
Yes. But then of course *every* subclass needs to define the method, doesn't it?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
I've always felt the exact opposite.Originally posted by marc weber:
My understanding is that RuntimeExceptions should not be used for situations that could be avoided by proper coding.
My take is that unanticipated errors due to programmer mistakes cannot be handled in code; they should be logged so you find them during development.Error!
The software you are using was written by someone who cannot follow instructions. Please terminate the program for your own safety.
Originally posted by David Harkness:
I've always felt the exact opposite.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by David Harkness:
Of course, that's precisely the point. When the method is optional, I use the UnsupportedOperationException route instead of making it abstract.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
|