aspose file tools*
The moose likes Beginning Java and the fly likes Extend or not extend Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extend or not extend" Watch "Extend or not extend" New topic
Author

Extend or not extend

L. Riley
Greenhorn

Joined: Mar 20, 2001
Posts: 28
I am importing the java.applet.* and java.awt.* packages in a program.
Please explain why I then have to add the 'extends Applet' to my class header but I don't have to add 'extends awt'.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
it is simply a mattar of the difference between packages and classes.
when you use import in the beginning you actually just import the package the clases are in.
for example: there is a package named java.awt in whcih there are many classes.
you import that package so you can use its classes more easily!
if u need to create a button you could type:

if u use the import.
otherwise without the import you need to name the whole package.

yikes!
the applet thing is the same idea. you import a package named java.applet (notice the small a) which has a class inside named Applet (capital a).
you dont have to import that package but if u dont u have to extend java.applet.Applet rather than Applet.
also, when u extend a class its called inheritence (thus you inherit all its methods etc)
hope i made it a little bit more clear.
im sure you get a bette answer by some moderator later.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
When you import a package, you are just telling the compiler where to go to find the classes that you are going to be USING in this class. Either you will be making an instance of something in these packages and saving a reference to it in one of your fields, or you will be using some of their classes static methods or variables, or whatever. Only the classes that the compiler actually needs get pulled into the final result.
Any instances of this class will have the pulled in classes as part of it's composition. The class that you are defining will not be able to modify the behaviors or definitions of the variables of the classes that it uses.
When you extend a class, you are telling the compiler that this class is going to BE a subclass of the extended class - a much bigger issue. Any instance of this class will inherit all of the behaviors and variables of the extended class unless you modify those by overriding them. This is where polymorphism comes in.
FYI - there is a premise of Object Oriented programming that says "favor composition over inheritance".


"JavaRanch, where the deer and the Certified play" - David O'Meara
L. Riley
Greenhorn

Joined: Mar 20, 2001
Posts: 28
Thanks. I think I'm beginning to understand now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extend or not extend