• Post Reply Bookmark Topic Watch Topic
  • New Topic

confusion in code regarding inheritance according to java5  RSS feed

 
kapilg gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Plant{
String getName() {return "plant";}
Plant getType () {return this;}
}
class Flower extends Plant
{
Tulip getType() {return new Tulip();}
}
class Tulip extends Flower{}

//This code is compiling in java5 according to book i have java4 compiler with me so i have not compiled it i cannot understand how can Tulip be used in its superclass because superclass doesn't know about its base class plz explain this
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is showing a new feature in Java 5, called "covariant return types".

I don't understand your doubt, though, because it's the subclass that knows about Tulip, not the superclass.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Ilja Preuss means about covariant return type is that in J5 and later, you can have an overridden method with a different return type, provided the return type is a subclass of the original return type.
 
kapilg gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
The code is showing a new feature in Java 5, called "covariant return types".

I don't understand your doubt, though, because it's the subclass that knows about Tulip, not the superclass.


yes i know about covariant return types what i want to ask is that when this program runs first class plant will be declared then class Flower
now in class Flower we have used Tulip return type but class Tulip is yet
to be declared according to the sequence of the program
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I think now I understand the question...

The class declarations aren't executed sequentially. As far as I know, you can declare them in any order you want, the compiler will sort it out for you.

At runtime, every class will reside in its own .class file, and the JVM simply loads the ones it needs. It doesn't know anything about class declaration order at all.

Does that help?
 
kapilg gupta
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Ah, I think now I understand the question...

The class declarations aren't executed sequentially. As far as I know, you can declare them in any order you want, the compiler will sort it out for you.

At runtime, every class will reside in its own .class file, and the JVM simply loads the ones it needs. It doesn't know anything about class declaration order at all.

Does that help?



Ya thanks a lot You provided the exact solution for which i was looking thanks
 
Rajesh Pitty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notice the lock between flower class and plant class
the flower class wont compile until plant class is compiled and vice versa.

so how does the compiler go about this.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Pitty:

so how does the compiler go about this.


This is only hard to imagine if you think of a compiler as a sort of translator that turns each symbol in a Java source file into a few bytes in a .class file, one after the other, in order. It doesn't work that way at all! A compiler like javac works in multiple stages. The early stages build up a data structure in memory to represent the contents of a .java file; the later stages read this data structure and emit the .class files. When the compiler sees the Flower class, it just makes a note that there needs to be a class named Tulip somewhere. It doesn't need any more information than that. By the time the later stages get around to emitting code, the structure representing the Tulip class has been all filled in.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!