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.
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:
- I have very little info on your classes so if the extending doesn’t make the code more readable you shouldn’t do it.
- All methods must be virtual in java so I would guess they have highly optimized the runtime dispatching of them.
- 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:
- “100’s” is a very small number in software engineering, you likely won’t see any performance slowdown at that few objects
- 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
1