1

I have a situation where multiple classes can extend one core class. There's a property (say, isAtomic), which in some subclasses will depend on the data but in other subclasses will constantly be true. I'd like, if possible, the check to be as inexpensive as possible in the latter case. I can think of several approaches:

  1. The obvious: abstract bool isAtomic() which would be implemented to return true/false in the "constant" subclasses,

  2. A subclass Atomic extends Base which would implement isAtomic() as return true; and which the other "constant" subclasses would extend (probably not too much different from 1),

  3. The base class implementing isAtomic as return true; and the non-constant subclasses overriding this definition,

  4. An interface Atomic implemented by the atomic subclasses which would obviously not return anything but would be checked against using x instanceof Atomic || x.isAtomic().

I'm not too experienced in OOP but something tells me that the last approach represents the concept nicely and saves a function call, even though the more complicated usage. But I don't know whether an instanceof is indeed "cheaper" than calling a function. 3 should be quicker than 1 or 2 but feels against the nature of the isAtomic function. What do the experts say? Or is there another intuitive way, better than those listed, I didn't think of?

The Vee
  • 11,420
  • 5
  • 27
  • 60
  • 1
    You could declare `isAtomic()` in an `interface` with a default implementation, if you're on Java 8. – Mick Mnemonic Mar 06 '16 at 11:15
  • FYI, #4 is known as the [*marker interface*](http://stackoverflow.com/questions/25850328/marker-interfaces-in-java) pattern. – Oliver Charlesworth Mar 06 '16 at 11:15
  • @MickMnemonic: Does that provide any practical difference from #3? In those classes which are not inherently atomic I would still have to implement the same interface and override the function, right? – The Vee Mar 06 '16 at 11:24
  • There is a big difference between extending from an abstract class and implementing an interface. You can only extend one class but implement multiple interfaces. Which one is better, depends on your code base. – Mick Mnemonic Mar 06 '16 at 11:40
  • `instanceof` is typically much more expensive than a function call. – arcy Mar 06 '16 at 11:47

1 Answers1

1

Go with your first choice. No one here can tell which of the options is the best for your particular case. If the first choice turns out to be a bad one and you have to revisit it, you'll have learned something.

As for efficiency, the JVM may inline calls to short methods. This can happen even if you are calling an abstract method, especially when the call site is monomorphic. Performance optimization is very difficult, so don't worry about it until you know you are going to have a problem.

I have a situation where multiple classes can extend one core class.

I know nothing about your program, but may want to avoid situations like this.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Thank you! Didn't know inlining could happen with abstract methods, that makes the situation very easy. Could you just please elaborate on the last line (for future ref: avoiding many classes extending a common base)? – The Vee Mar 06 '16 at 12:31
  • Inheritance is often misused, leading to programs that are hard to understand and difficult to modify. You can find many articles and long discussions on this topic and what alternatives exist, e.g. search the internet for "composition over inheritance" or http://programmers.stackexchange.com/questions/260343/why-is-inheritance-generally-viewed-as-a-bad-thing-by-oop-proponents – Joni Mar 06 '16 at 13:21
  • Oh, I see. I know well of that topic and have taken it into account when designing the model. Thank you, the first choice is what I picked then! – The Vee Mar 06 '16 at 21:20