Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we exteds object

 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

can smbdy pl. tell me why object class gets extend by default in all class we create in java. what is the signifecance of making object class as the parent of all classes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because all classes extend java.lang.Object, all the methods of java.lang.Object are available on every object. That means basic facilities like comparison for equality, conversion to a String, etc., are available for every class. If there were no single root to the hierarchy, then it would not be possible to write a method that accepts any object as a parameter, or a Collection that holds any kind of object.
 
Ram Hebbale Hiriyanna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets not forget....java is an object based programming language! as the sheriff says, Object gives you methods for many things.....also for thread handling, object state description, etc.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If there were no single root to the hierarchy, then it would not be possible to write a method that accepts any object as a parameter, or a Collection that holds any kind of object.

This is not true. Once you step outside of the bounds of Java (by saying "there were no single root to the hierarchy"), then it is certainly possible do "write a method that accepts any object as a parameter".

To answer the original poster's question, http://contractualj.com/api/net/tmorris/AntiObject.html
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

Once you step outside of the bounds of Java (by saying "there were no single root to the hierarchy"), then it is certainly possible do "write a method that accepts any object as a parameter".


Tony, the example you've linked to seems to replace a universal base class with a universal interface; without discussing the merits of that, I don't think it materially changes this discussion. In a strongly-typed language, to write (for example) a hash map which allows arbitrary objects for keys, the fact remains that you need a common base type. The alternatives are "duck typing," or lexical templates as in C++ that give the effect of duck typing.

But Sachin, Tony is right to suggest that Java could have been designed differently, in which case the common base class wouldn't have been needed.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it has something to do with the Object concept in java. Everything in java is an object. This seems like obvious but when you think that the Class object is an Object, it seem logic that the object concept in java has more weight than the class concept.

The Object being the father of everything assures some java functionalities like reflection, serialization, and other internals. It is also the fundamental difference with other oop languages. (Other languages take class as the fundamental concept in the language).

It also helps to simplify the language implementation, by having a Object that is father of other object you assure that only exists a root in the class tree. So all object inherit the zero-parameter constructor, and util methods like toString. This makes a lot easier to handle the procedure of creating and destroying objects in the runtime.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


Tony, the example you've linked to seems to replace a universal base class with a universal interface; without discussing the merits of that, I don't think it materially changes this discussion.


I agree, an explanation is missing. That can be said for a lot of what is on the ContractualJ website. There is only one me.

Since the more precise answer of "because it is a defect of the language and the AntiObject interface is the optimal workaround" attracts criticism and request for explanation (understandably) that I am not prepared to provide, I thought that perhaps looking at ContractualJ would be meaningful enough. Certainly *using* ContractualJ provides some much deeper insight (there is a specific reason for this).

It is at least, more explanation than "the other camp" provides, which to me, is not much more than a religious subscription to the notion of "a common supertype is correct, because Java/.NET/Smalltalk has it". Ultimately, I encourage the OP to derive their own conclusions.

I am doing my best (I know I suck at it) to disengage from the fruitless arguments with the doctrine, while still portraying reality to those who request it.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Everything in java is an object."

That is not true, in some OO languages it is(smalltalk), not in Java.

int, float, boolean, ect...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you use instead of a common base type?

Tony, I enjoy reading your arguments because it's always interesting to see someone argue against dogma. That being said, challenging the doctrine is all fine and good unless the argument is based on terms and premises you cannot or will not explain and define. At that point it just becomes a rant and clouds what points might have been otherwise understood or even accepted.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
"Everything in java is an object."

That is not true, in some OO languages it is(smalltalk), not in Java.

int, float, boolean, ect...


You are right. There are things in java that aren't objects. And this makes me think why they aren't?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
What would you use instead of a common base type?

Tony, I enjoy reading your arguments because it's always interesting to see someone argue against dogma. That being said, challenging the doctrine is all fine and good unless the argument is based on terms and premises you cannot or will not explain and define. At that point it just becomes a rant and clouds what points might have been otherwise understood or even accepted.


I agree entirely - at this very moment, I am making it easier to write and publish articles on my website - I wish I had 8 arms more than you wish I did I promise I've also resigned from my position at IBM and plan to take up a new position soon so I have no idea what my personal time will be like.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
I agree entirely - at this very moment, I am making it easier to write and publish articles on my website - I wish I had 8 arms more than you wish I did I promise I've also resigned from my position at IBM and plan to take up a new position soon so I have no idea what my personal time will be like.


You must because I wouldn't wish that on anyone! If you're married then surely you've seen the lists they come up with when you've only got two arms, I'd hate to see the list you'd get if you had eight. Be hard to make excuses too, "Honey I would have done it but I just need another hand!" I don't think she'll buy that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic