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)
Code :
[class myclass {
//body of class declaration
}
The second general form of a class declaration is
Code :
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:
Code :
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.?
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
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.
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
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
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.