• 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

Can you explain package hierachy in java.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object class present in java.lang.package.
java.lang.package imported by default in java.
Object class contains Event handling classes and awt.

So why we need to import java.awt when we use awt in java...since it should be in lang.package which is imported by default...

please let me know where i am wrong...

What I have tried:

tried seeing oracle documentation of hierarchy.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A washing machine may be running embedded Java and has no need for GUI code.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Barry Singh Allen wrote:. . . .
Object class contains Event handling classes and awt.
. . . .

No, it doesn't.

There is to all intents and purposes no such thing as a package hierarchy. Even when packages are inside each other, the included packages are not somehow “subordinate” to their enclosing packages. There is a difference in behaviour, but it is so siubtle that I have forgotten what it is. I think you are thinking about inheritance hierarchies. All classes including records, exceptions, and enums are subtypes of Object, That means they inherit Object's eleven non‑private members, which turn out all to be methods.
Importing:-
  • 1: Types in current package: automatically imported.
  • 2: Types in java.lang package: automatically imported if not already imported. That is why it is such a bad idea to create a class with the same name as one you are about to import.
  • 3: Classes in every other package: names not already imported must be explicitly imported or must use the fully qualified name of the type.
  •  
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I merged your stuff with the following thread. I hope that is okay by you.
     
    Barry Singh Allen
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i am learning awt and i am confused in java hierarchy.
    So since java.lang imported by default and it contains Object class.
    Object class is base class to all class.

    So when lang package is imported by default, will all the derived class of object class be also imported ?

    What I have tried:

    tried seeing oracle documentation of hierarchy.
    read about import keyword.
     
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No, only the types contained by java.lang are imported by default.

    The concept of importing is not very difficult: if you want to refer to a type by their simple name, you must import them.

    Example:

    In this example, both String and ArrayList are referred to by their simple name, so they need to be imported. String is imported automatically because it's in java.lang.

    ArrayList needs to be imported explicitly.

    Here are two examples where you don't have to import ArrayList:

    In the first example, you referred to ArrayList by its fully qualified name, not its simple name.

    In the second example, you didn't refer to the ArrayList class by name at all.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Remember that a superclass never “knows” anything about how many subclasses it has. Only the programmers know that.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic