• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extend or not extend

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'.
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
L. Riley
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I think I'm beginning to understand now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic