• Post Reply Bookmark Topic Watch Topic
  • New Topic

inhertance question / interface /abstract class  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!