File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Class" Watch "Object Class" New topic
Author

Object Class

namrata mahajan
Greenhorn

Joined: Jan 08, 2009
Posts: 6
it may sound silly. but need to clear this point guys.
Why Object class is the base class of all classes???
kindly reply please.... :lol:
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Namrata,

I know that the answer might sound silly too, but: Is there an option? The option would be I guess that you wouldn't have Object, and then every class you write would have to implement the Object methods like toString(), clone(), notify(), wait(), equals(), hashCode(), etc. It makes sense to have a global superclass that encompasses everything that all other classes should have, and that's why you have Object. If you didn't have Object you would have to write hundreds if not thousands of lines of code for every one of your hierarchies' superclasses.

But it's good that you think in the most basic terms, because many people take things for granted and don't try to figure out why things are the way they are, so actually it is an excellent question.


All code in my posts, unless a source is explicitly mentioned, is my own.
namrata mahajan
Greenhorn

Joined: Jan 08, 2009
Posts: 6
Thanks for your reply ruben.
But there are some methods in the object class which have body and some methods which dont have body.Why is it so?

Kindly elaborate.

Thank you.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

namrata mahajan wrote:
But there are some methods in the object class which have body and some methods which dont have body.Why is it so?



Lets called them the "default implementation" of that method, like for example, Object#toString() method's default implementation is the string concatenation of


i.e class name + hash code for that object.

So If you don't want this weird string to be returned , you can override Object#toString() method and provide your own implementation.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

And Yes, About, those who don't have any body, like , hashCode, clone method, I think they are more class specific and their behavior mostly depend upon the that particular class.

Lets others Ranchers put some more light on it..
srikanth mycherla
Greenhorn

Joined: Oct 31, 2008
Posts: 26
does the object class has default constructor like this

public object()
{
}
namrata mahajan
Greenhorn

Joined: Jan 08, 2009
Posts: 6
Thanks.

In an Object class some methods are abstract and some are concrete.But Object class is a concrete class.Then why object class is having abstract methods.Why object class is not abstract class?

In Object class there are notify() and notifyAll() methods which are abstract and we use them without writing method body in our code and they perform the task. so my question is how they do the task is it that they have been actually defined somewhere???



Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
namrata mahajan wrote:
In Object class there are notify() and notifyAll() methods which are abstract and we use them without writing method body in our code and they perform the task. so my question is how they do the task is it that they have been actually defined somewhere???

Where did you read that?
I cannot find where class Object methods are abstract :roll:

Instead I see that they are final......http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

srikanth mycherla wrote:does the object class has default constructor like this

public object()
{
}


I want you to unzipped src.zip file, that's comes with JDK installation. And open the Object.java file, that's helps you to find out your most of the question about Object class.
namrata mahajan
Greenhorn

Joined: Jan 08, 2009
Posts: 6
hi Sachin,

There are some methods which are not final. e.g



and most of them are final. e.g



but this method is final with no method body if i am not wrong. then where is the method implementation for this method has been defined.??

namrata mahajan
Greenhorn

Joined: Jan 08, 2009
Posts: 6
Hi shrikant,

I have gone through src.zip folder.There is no default constructor for object class.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

namrata mahajan wrote:
In an Object class some methods are abstract and some are concrete.But Object class is a concrete class.Then why object class is having abstract methods.Why object class is not abstract class?

In Object class there are notify() and notifyAll() methods which are abstract and we use them without writing method body in our code and they perform the task. so my question is how they do the task is it that they have been actually defined somewhere???


Look at the source of Object class. The method who's body is empty, are declared as "native", and "native" methods implementation is not Java code, its underlying OS dependent.


Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
namrata mahajan wrote:Hi shrikant,

I have gone through src.zip folder.There is no default constructor for object class.


No need to write default constructor, compiler is your good friend it will add for you and call for you too. Dip in more in java, you will understand many hidden things.

By the way excellent answer Ruben. And there is no abstract method in Object class, otherwise how will you do this:



Designers must have given lots for time to design Object class, they had to think which methods will be required by most of the childs of Object, what should be kept common to all, what should be kept final.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Class