• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dependency between String and Object class in Java  RSS feed

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just came this question into my mind.

As Object is superclass of all classes in Java, so is the superclass of String class.

But Object Class has toString method in it which has return type of "String" (its child class). And String is the class with Object as parent class. So how this circular dependeny got managed at the time of creating initial version of Java by Java creators. How the compiled Object class against its dependency with its child class?

Did they removed the toString method initially to compile Objecl class successfully and then after compilation of String class against properly compiled Object class they added toString method to Object class back and compiled Object class again with compiled String class?

This question just came out of curiocity in my mind. May be stupid question/answer would be stupid.??

Regards,
Pras
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pras Kumar wrote: how this circular dependeny got managed at the time of creating initial version of Java by Java creators.

circular dependeny ? there is no circular dependeny . if child class dont want a method from it super class , sub class can be defined the particular method in it. it is called overriding.
 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Seetharaman,

My query is as the parent class (Object) is having method toString which has return type of Child class (String). How it has been at the time of initial writing of Object Class?

public String toString() {
......
}

How dependency got resolved at the initial time while writing Object class? As both class has reference to each other.?

Also as Object is parent class of String in String Definition fron Java Souce code

public final class String implements java.io.Serializable, Comparable, CharSequence{
....
}

there is no "extends Object" there? Why so?

Regards,
Prashant
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pras Kumar wrote:Also as Object is parent class of String in String Definition fron Java Souce code

public final class String implements java.io.Serializable, Comparable, CharSequence{
....
}

there is no "extends Object" there? Why so?

If there is no explicit extends clause, the class implicitly extends Object. It's probably allowed to omit it so you don't have to type "extends Object" for every "top level" class you create.
 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

But any iodea about this?

- as the parent class (Object) is having method toString which has return type of Child class (String). How it has been at the time of initial writing of Object Class?

public String toString() {
......
}

How dependency got resolved at the initial time while writing Object class? As both class has reference to each other.?


Regards,
Pras
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how they did it, but the Java compiler doesn't care about circular dependencies. I can write two classes in different files that reference each other and still compile them both.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You simply have to compile both simultaneously to avoid a circular dependency.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You simply have to compile both simultaneously to avoid a circular dependency.

Compiling any one of the file is enough. compiler automatically compile the reference class.


and when you use javac test\A.java ; compiler also compile B file.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote: . . . and when you use javac test\A.java ; compiler also compile B file.
Yes, you are right. You might, however, have to compile them simultaneously when the two files are intended for different packages.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!