You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Donald Jolley <jo...@gmail.com> on 2011/09/10 23:54:56 UTC

Do imports inherit?

I am writing a domain registration application.Typically, servlets
extend the HttpServlet class and import java.io.*, javax.servlet.*,
and javax.servlet.http.*.  There are some methods and variables that I
want to make available to all of my servlets in this application.  So,
I decided to create a DomainRegBase class which extends HttpServlet
and imports java.io.*, javax.servlet.*, and javax.servlet.http.* just
like a servlet would.  I then add my methods and variables to the
DomainRegBase class and have all of my servlets extend from the
DomainRegBase class rather than from the HttpServlet class.All seems
to be working well.  However, I was a bit surprised to observe that in
order to get my individual servlets to compile I need to import
java.io.*, javax.servlet.*, and javax.servlet.http.* into each of my
individual servlets.  I was sort of thinking that if I imported those
classes into a parent class that would be sufficient for all the
children.  Apparently it isn't.  Am I missing something?  Thanks.
      ... doug

Re: [OT] Do imports inherit?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Doug,

On 9/11/2011 12:24 PM, Donald Jolley wrote:
> Thanks for the clarification.
> 
>> Also note that it's generally frowned upon to import with
>> wildcards; be
> specific with the classes you use.
> 
> Your suggestion certainly seems reasonable to me.  I do, however,
> note that the use of wildcards seems to be fairly prolific.  In
> this particular case my use of the wildcards was simply a case of
> my following a textbook example.

Wildcards are often used for brevity in examples, etc. because it's
important for the reader to know what the imports are, but there's no
reason to print 50 import statements.

As for practical matters, the compiler runs a bit faster when it only
"imports" a few classes instead of a whole package. Also, wildcard
imports can often lead to conflicts: for instance, both packages
javax.xml.stream.events and org.w3c.dom have a class called
"Comment"), so if you wildcard-import them, both, you'll have to
fully-qualify the use of "Comment" in the source code.

Also, since the Java API isn't static and new classes are added with
each release, you might find that "import java.io.*" in version 1.2 of
the API works just fine, but that when you upgrade to version 1.7, you
find that there is a new class that conflicts with one where there was
no historical conflict and suddenly your code no longer compiles
without changes. What looks like a bug in your code is actually just
laziness of the programmer that came back to bite you later in the
code's life.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk5utJIACgkQ9CaO5/Lv0PDn4QCgqUoWn702LxDrMOiSKYRBp+/Q
PBUAn06IkpcRMZPDcdtlrFY9nTbI+ZHo
=EQMu
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Do imports inherit?

Posted by Donald Jolley <jo...@gmail.com>.
Thanks for the clarification.

> Also note that it's generally frowned upon to import with wildcards; be
specific with the classes you use.

Your suggestion certainly seems reasonable to me.  I do, however, note that
the use of wildcards seems to be fairly prolific.  In this particular case
my use of the wildcards was simply a case of my following a textbook
example.

Thanks for the input.

        ... doug

RE: Do imports inherit?

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Donald Jolley [mailto:jolleytime@gmail.com] 
> Subject: Do imports inherit?

> I was sort of thinking that if I imported those classes 
> into a parent class that would be sufficient for all the
> children.  Apparently it isn't.

Your final conclusion is correct - it's not sufficient.  This is a basic characteristic of Java; if a class explicitly uses a type, the type must be declared in that class.

Also note that it's generally frowned upon to import with wildcards; be specific with the classes you use.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org