Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a class with a lot of methods. I would like to group similar methods together in their own class, but all of the methods need to extend another class, ClassC. So I was thinking of having ClassA, which contains the first group of methods, extend ClassB, which extends Class_C, etc.

Is this inefficient in terms of runtime performance, or are they virtually the same?

Note: there will be hundreds of instances of this class running at once, so I would really not want to waste memory.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Technically virtual methods will slow a program down because a runtime check is performed to decide which method to use. However you should consider these things before worrying about it:

  1. I have very little info on your classes so if the extending doesn't make the code more readable you shouldn't do it.
  2. All methods must be virtual in java so I would guess they have highly optimized the runtime dispatching of them.
  3. Lastly you won't know how much the program is slowed down until you actually try it, generally worrying about small optimizations before you need to optimize is a bad idea

A couple more things that require some assumption on my part:

  1. "100's" is a very small number in software engineering, you likely won't see any performance slowdown at that few objects
  2. While multiple extension might not slow down your program in general programmers like to save inheritance for is-a relationships, in your situation it might be better to use composition instead of inheritance
share|improve this answer
    
Thanks (for the second #1). I just wanted to be sure. –  TAAPSogeking Sep 1 '13 at 4:47
    
+1 - The calls are extremely optimized by the JIT - and if you keep methods small and interfaces simple you tend to get methods inlined and monomorphically dispatched anyhow (no/less lookups). –  Martijn Verburg Sep 1 '13 at 8:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.