File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes why not inherit imports? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why not inherit imports?" Watch "why not inherit imports?" New topic

why not inherit imports?

Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
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...
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

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.
Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1744
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.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

I stand corrected... (actually sitting at the moment, but you get the idea)
Matt Horton
Ranch Hand

Joined: Feb 06, 2002
Posts: 107
Thanks everyone...
It makes a bit more sense to me now; I guess I understood the rationale before I wrote the question...
I was just frustrated so I asked it anyway.
I agree. Here's the link:
subject: why not inherit imports?
Similar Threads
why fields are constant in Interfces?
Instance variable access
Why these imports in AdviceClient class?
extending a class and importing packages
Why do we have concrete methods in abstract class ?