aspose file tools*
The moose likes Beginning Java and the fly likes Is importing a class same as extending a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is importing a class same as extending a class?" Watch "Is importing a class same as extending a class?" New topic
Author

Is importing a class same as extending a class?

Nikhil Reddy Lingala
Greenhorn

Joined: Dec 06, 2007
Posts: 15
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{
}
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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."


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Dec 06, 2007
Posts: 15
I got the answer.
Thankyou all..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yes Rob. Very true.
npk chowdary
Greenhorn

Joined: Dec 09, 2007
Posts: 21
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is importing a class same as extending a class?