Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Object class( pardon me for the long quotations please)

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sri Lanka
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Object class( pardon me for the long quotations please)

    I'm confused with Objects and "Objects"' class as explained in one of the following Java tutorials I found in the Net.

    It says:
    Usually you will declare classes in Java using one of two general forms. The first general form of a class declaration is (using myclass as an example)

    [class myclass {
               //body of class declaration
    }

    The second general form of a class declaration is
    class myclass extends extendedclass {
               //body of class declaration*
    }
    I can understand that in the second form of class declaration, myclass inherits from the extended class. If myclass doesn't inherit from a class the first form is the default way of declaring a class.

    But the tut goes on to say:

    If myclass does not extend another class, myclass class extends the Object class and the class declaration can be written,if necessary, as:

    class name extends Object {
               //body of class declaration
    }

    I'm now confused with this Object class. can anybody explain me, please. I hope my problem description is clear enough.?


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: Object class( pardon me for the long quotations please)

    Check this

    may this will make you understand some.

    and for quick reference
    try this with your bytecode

    javap -c <classname>

    eg:

    javap -c myclass

    see what compiler added in your code
    Last edited by DanBrown; February 4th, 2011 at 01:57 AM.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Sri Lanka
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Object class( pardon me for the long quotations please)

    Thanks so much Dan for your reply. I'm trying to digest contents in the sources you have pointed.

    Regards.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Object class( pardon me for the long quotations please)

    Ok, so are you confused about the Object class (Object (Java Platform SE 6))?

    For the sake of not confusing the differences between the Object class and an object, I will refer to the Object class by its library (java.lang.Object) and an object by just "object".

    If the java.lang.Object class is confusing you, the java.lang.Object class is effectively the "god-class" of java. EVERY object in java extends java.lang.Object by default. By extending java.lang.Object, every object gains access to the java.lang.Object class's methods.

    Now, in java, every time you make a class, you are making a new object. Every time you call a method, you are calling a method of an object.

    For example, String is an object (String (Java Platform SE 6)). Since String is an object, it extends java.lang.Object. The String class contains something like a hundred or so methods that are specified in its API. But apart from the methods specified in its API, it also has access to all of java.lang.Object's methods that String has not overwritten (when an object wants to use an inherited method differently than how it is already declared). So, String also has access to 8 of java.lang.Object's methods. Since String has inherited these methods instead of overwriting them, if you call one of java.lang.Object's method from a String, it will use java.lang.Object's declaration.

    Inheritance can be difficult to understand. When an object (Object_A) extends another object (Object_B), Object_A is an Object_B, but a different version of Object_B with added functionality. But here's the catch, Object_B is not aware that Object_A is a "child" of it, but Object_A is aware that it is a "child" of Object_B.
    So, have a look at this:
    Example 1)
    Declaration
    class Component
    {...}
    Visual
    Component -> java.lang.Object
    Explanation
    The above says that Component is an java.lang.Object. As you can see by the direction of the arrow, Component can see java.lang.Object, but java.lang.Object cannot see Component.

    Example 2)
    Declaration
    class Button extends Component
    {...}
    Visual
    Button -> Component -> java.lang.Object
    Explanation
    So, Button extends Component, so Button is a Component. But, Component extends java.lang.Object, which means that Component is an java.lang.Object. This means that Button is also an java.lang.Object, since Component is an java.lang.Object and Button extends Component. Also, Button is aware that it is a Component. It is also aware that Component is an java.lang.Object, so it is aware that it is also an java.lang.Object. Component is aware that it is an java.lang.Object, but it is not aware that Button is a Component or an java.lang.Object. Basically, Component doesn't even know that Button exists and java.lang.Object doesn't even know that Component and Button exist.


    Tell me how much sense all that makes. Read it slow and try your best to understand it. Inheritance is very important to Object-Oriented programming.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    JavaPF (February 4th, 2011), nimsri (February 4th, 2011)

Similar Threads

  1. Object as a Reference into Object's Class
    By Ace Coder in forum Object Oriented Programming
    Replies: 6
    Last Post: November 30th, 2010, 12:22 PM
  2. Converting Object to Long
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: August 20th, 2010, 08:35 AM
  3. Object or Class?
    By mortalc in forum Object Oriented Programming
    Replies: 23
    Last Post: August 20th, 2010, 01:17 AM
  4. Class and Object
    By jyothishey in forum Object Oriented Programming
    Replies: 2
    Last Post: January 25th, 2010, 08:48 AM
  5. Replies: 2
    Last Post: November 5th, 2009, 10:15 PM