This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, this is a naive question... but why do subclasses not inherit their superclasses' imports??? I realize there's kind of a mechanistic reasoning... the imports are in the header of the superclass rather than in the definition of the class itself... but it still seems as if it would be awfully pragmatic for subclasses to inherit their parent's import set.... Thanks in Advance...
Well a subclass doesn't have to use all the same objects and classes as the superclass. So it could cause a lot of unneccesary overhead to force EVERY subclass to also load in all the imported classes as the super class.
I don't think import statements actually cause classes to be loaded. They just allow you to refer to classes without the full package name. Regarding the original question, imports don't "belong" to a class really. They're just a feature of the compilation unit, not of any particular class in that compilation unit. (A compilation unit can contain multiple top-level classes, but only one top-level public class, right?) Also, refering to the stuff in a compilation unit that comes before class definitions as a "header" might lend to it some special significance it may not have in Java.
Extra import statements don't cause packages or classes to be loaded - they might cause the compiler to think for an extra microsecond, but I'm not sure of that. The import statements are only in the source code - they aren't in the bytecode and so inheriting a superclass's import statements is meaningless as you don't inherit source code - you inherit bytecode.