Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between extending class, importing package

 
Keyur Jinwala
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic