Forums Register Login

inhertance question / interface /abstract class

+Pie Number of slices to send: Send
I am trying to model a quiz. I create a Quiz object. There are 4 different types of Quiz. Each quiz needs to calculate results...therefore each of the 4 Quiz objects should have a calculate() method. Because each has to have this method, I thought I would create either an abstract class or interface Quiz and have a calculate method that would need to be defined in each of the concrete class. The problem though is that for the calculate methods the parameter passed would be of a different type in certain cases. In 2 cases I want to pass in a List and in another case I pass in a Map object. Since the parameters are different I can't define the method in the superclass, unless in the superclass I define the parameter as Object. Is this the way to go or is this not a case for using inheritance?

Thanks.
+Pie Number of slices to send: Send
Why are the quizes so different that they require individual classes? You sound like you are creating 4 different types of quiz classes, but why not create 1 quiz class with 4 different instances?

In the case you do in fact need 4 different classes, an interface plus a helper class is a good pattern, or in second place an abstract class that all the quizes extend. There's very few limits about what you can come up with as far as inheritence goes. Classes that implement specific methods can exit without doing any work, although this type of implementation makes me think their's something wrong with the design.

In the case of map/list parameters, there are additional collection classes that could replace both such as a SortedMap.
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 821 times.
Similar Threads
Inheritance: Abstract or Interface
Inheritance
Interfaces and abstract classes
IBM OOAD Practice Exam - Which 2 are wrong?
abstract class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:19:47.