aspose file tools*
The moose likes Java in General and the fly likes why we exteds object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why we exteds object" Watch "why we exteds object" New topic
Author

why we exteds object

sachin yadav
Ranch Hand

Joined: Nov 24, 2005
Posts: 156
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ram Hebbale Hiriyanna
Greenhorn

Joined: Dec 07, 2005
Posts: 10
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.


SCJP 1.4
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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


Tony Morris
Java Q&A (FAQ, Trivia)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Mar 28, 2005
Posts: 133
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.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Jan 03, 2006
Posts: 490
"Everything in java is an object."

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

int, float, boolean, ect...


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Mar 28, 2005
Posts: 133
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

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Jul 15, 2003
Posts: 1078
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why we exteds object