• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is importing a class same as extending a class?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all..
I have a doubt as to what happens to a class when we import an another class in some package... I have read some where that importing a package saves us from lot of typing.. does that mean.. the code of the class that we are importing get adds up in the class we are writing??

does the code bits below represent the same thing?

//code bit 1
import java.util.Date;
class MyDate{
}

//code bit 2
class MyDate extends java.util.Date{
}
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nikhil Reddy Lingala:
Hello all..
I have a doubt as to what happens to a class when we import an another class in some package...



Howdy Nikhil Reddy Lingala,Welcome to JavaRanch

When you import a class in another class, the class which has the import statements gets the approval of making use of the class being imported.

That means, you intimate the compiler saying that "I know class A is being used in my program. I am telling you that the Class A beloging to the package x.y.z is what you have to look for the syntax when compiling".. The same holds good for the Java Runtime Environment when the program is in execution.


I have read some where that importing a package saves us from lot of typing.. does that mean.. the code of the class that we are importing get adds up in the class we are writing??



Yes, that is true. In some cases you don't have to type the fully qualified class name wherever you want to make use. For example, instead of typing



everywhere in your program, you can just type



In order to achieve that, you need to import the class or the package name where this class belongs to. The reason is explained in the previous para.



does the code bits below represent the same thing?

//code bit 1
import java.util.Date;
class MyDate{
}

//code bit 2
class MyDate extends java.util.Date{
}



No.

When you import, you let the Java environment just aware of the class and the package it belongs to.

Whereas when you extend, your class is getting access to all the available, visible members of the class you are extending. It is "inheritance".

Just read about inheritance here you will get to know about it more.
[ January 01, 2008: Message edited by: Raghavan Muthu ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imports are actually just a shortcut. When you import java.util.List, you tell the compiler "wherever I refer to a class called 'List' in this code, I actually mean 'java.util.List' (instead of java.awt.List, for example). I'm just too lazy to type the fully qualified name everytime."
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, you have to type the fully qualified class name when you have both the java.util.List and java.awt.List classes in your program
 
Nikhil Reddy Lingala
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the answer.
Thankyou all..
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raghavan Muthu:
Remember, you have to type the fully qualified class name when you have both the java.util.List and java.awt.List classes in your program


Only for one. You can decide to import java.util.List and reference to that as just List, and not import java.awt.List and reference to that with the full name including package.

It can be annoying sometimes - not only with List, but also with FileFilter. There's one in java.io and one in javax.swing.filechooser.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Rob. Very true.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------

I have read some where that importing a package saves us from lot of typing.. does that mean.. the code of the class that we are importing get adds up in the class we are writing??

--------------------------------------------------------------------------------
yes it saves us lot of time but it is different in approachit cannot add the code i.e as part of import statement.when the hotspot jvm encounters the code which uses your importing statement it will think like ohh i have to go there and execute those set of code and return the result here.thats it.no code is added
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are thinking about static import.

There is a description of static import in the Java Tutorial. [They say to use it sparingly, but I can't see that it would do any harm to import lots of things provided there is no name duplication.]

Static import allows you to "import" static members of a class and use them as if they were (static) members of your class. Like this:
It would have been better to use the toDegrees() method, but I have shown you can import methods and constants.
reply
    Bookmark Topic Watch Topic
  • New Topic