File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not inherit imports?

 
Matt Horton
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
whippersnapper
Ranch Hand
Posts: 1796
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected... (actually sitting at the moment, but you get the idea)
 
Matt Horton
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic