• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Class

 
namrata mahajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
namrata mahajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the object class has default constructor like this

public object()
{
}
 
namrata mahajan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shrikant,

I have gone through src.zip folder.There is no default constructor for object class.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic