• 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

Object Class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???



 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic