File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Diiference betwwen importing a class and extends a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Diiference betwwen importing a class and extends a class" Watch "Diiference betwwen importing a class and extends a class" New topic
Author

Diiference betwwen importing a class and extends a class

swarupa patil
Ranch Hand

Joined: Feb 22, 2006
Posts: 62
Hi Ranche
I like to know the difference between the importign a class and extending a class .
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

Hi above,

Your question is too vague. Please be specific.
Think of the word inheritance when extending the class which you dont get when importing.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
Hi,

'import'is used to tell the compiler where to look for a class defined in a package.

'extends' is used to inherit properties from a class. Class from which we inherit is called 'base'(parent) class and the resultant new class is called 'derived'(child) class.

Lets say class A is defined in package P,and you are writing a new class B in another package Q, where you want to
'extend' class A.

In this case you write the code as below:



As can be seen from above, 'import' is used to tell compiler where to look for class A and 'extends' is used to create a new class (here B) using an existing class (here B).

Hope I didnt confuse you.
[ October 11, 2006: Message edited by: Rajesh Kadle ]

-Raj
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218

'import'is used to tell the compiler where to look for a class defined in a package.

I differ, import is more of a programming convenience, than a neccessity. Your program is very well off without the import statement.

For eg. instead of an import, you could very well have used the fully qualified named of the class as P.A wherever you need to refer to A.

An import in this case is only useful for referring the class A in package P by its simple name.

For example:

So you see, it works without an import too, with the right classpath of course!
[ October 11, 2006: Message edited by: Aniket Patil ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
The import statement is a little bit like the path statement in operating systems. It just makes stuff available, it doesn't do anything to bring it into your program. The import statement has a negligable (zero?)performance overhead. It is not, and I repeat NOT like the include statement in C/C++, and is entirely different to the extends statement.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Diiference betwwen importing a class and extends a class