Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 12
    All 3 classes now have to know about each other. Adding Sub3 would involve a lot of code changes, and adding Sub10 would be downright painful Commented Apr 29, 2015 at 19:21
  • 15
    It would help a lot if you gave us real code. There's situations where it's appropriate to take decisions based on the particular class of something, but it's impossible to tell if you're justified in what you're doing with such contrived examples. For whatever it's worth, what you want is a Visitor or tagged union. Commented Apr 29, 2015 at 21:07
  • 1
    Sorry, I thought it would make things easier to give simplified examples. Maybe I'll post new question with a broader scope of what I want to do. Commented Apr 29, 2015 at 21:13
  • 12
    You're just reimplementing casting and instanceof, in a way that requires lots of typing, is error-prone, and makes it hard to add more subclasses. Commented Apr 30, 2015 at 0:32
  • 5
    If Sub1 and Sub2 cannot be used interchangeably, then why do you treat them as such? Why not keep track of your 'sandwich-makers' and 'alien-contacters' separately? Commented Apr 30, 2015 at 8:10