Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java Object class

 
Gaurav Bohra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why Object is always a super class of all java classes.
what is the problem if java don't use Object as super class of all classes
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes you like to compare objects, which aren't from the same class.
You may have cats and mice.
Both are (implicitly) derived from Object.
Object has a method 'boolean equals (Object compared)'.
Since mice and cats are object, you may call

Both comparisions will return false, while 'equals' isn't overriden.
In this way, every method may expect a parameter of type 'Object', giving you a great flexibility.
Some methods are used for Thread-handling. (wait, notify, ...).
The toString () - Method is useful for debugging.
The getClass () - Method is used in reflection.
Look at the javadocs for the methods, and a good beginners-book for the description of the usage.
The second question can't be answered. You cannot remove 'Object' everything depends on it. To keep almost all the functionality you would need alternatives. Of course other oo - languages exist with other concepts, but that's beyond my level. Perhaps it's interesting, to have a look at 'C#' (looks very similar to me), which is a newer Language, and c++ which is older - c++ doesn't have an super-root: Object, and I don't know, how reflection is handled there.
Smalltalk and Lisp are other oo - languages to have a look.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic