wood burning stoves*
The moose likes Beginning Java and the fly likes Difference between extending class, importing package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between extending class, importing package" Watch "Difference between extending class, importing package" New topic
Author

Difference between extending class, importing package

Keyur Jinwala
Greenhorn

Joined: Jun 14, 2007
Posts: 3
What is the difference between extending a class and importing it as a package. If I want to extend class A, can I write import A?

What would be its impact compared to extending it?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Keyur Jinwala:
What is the difference between extending a class and importing it as a package. If I want to extend class A, can I write import A?

What would be its impact compared to extending it?


Importing a class and extending a class are two different concepts.Poles apart
.You have to import to make the class visible and you use extends for inheritance purpose.


Rahul Bhattacharjee
LinkedIn - Blog
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
[Definitely a beginner question - moderator may like to move it].

Importing a class is purely a syntactic convenience. It just saves you from typing the package in front of every class name.

Importing has no effect on semantics: if you didn't import anything and instead wrote out all class names in full, with their packages, the program would behave identically.

Extending a class is a completely different concept and one of the fundamentals of object orientation.
[ July 18, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

To put it more simply, when you import a package, you're basically telling the compiler "hey, if you don't know what some class is, you might find it in THIS place".

that's all you're doing. if you never used imports, you would have to do things like:

com.foobar.foo.bar.MyClass myVariable = new com.foobar.foo.bar.MyClass();

with imports, you do this (after using the import statement):

MyClass myVariable = new MyClass();


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between extending class, importing package
 
Similar Threads
what is the difference between importing and the extending statements
Extending vs Reference
exception relatead
Is there any difference in importing.
import vs extend?