• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why compilation of the code fails here

 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compilation in the following code fails and i am unable to figure out the reaon .
Can anyone please help me out in this.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you tell us what error message you get? And where it says the error is?
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line number 16 it showing 2 errors .
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem is with this line,


try the following code,
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ananth its working.. What was the reason of error ?
 
Ananth Chellathurai
Ranch Hand
Posts: 349
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There were 2 reasons.
1. You were trying to cast a String to Fractal object.
2. Return type of the getOb() method is Object. Object class does not have getMsg method in it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyya Joshi wrote:At line number 16 it showing 2 errors .

Again, it is of great help if you actually post the text of the error message. Those error messages tell you a LOT about what the problem is. They are not just there for decoration.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred i am getting following error


Fractal.java:17:cannot find symbol
symbol:method getMsg()
location:class java.lang.Object
String s=(Fractal)ft.getOb().getMsg();

second error

java:17:incompatible types
found:Fractal
required :java.lang.Stiring
String s=(Fractal)ft.getOb().getMsg();


I hope this make sense.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ananth i think getOb() at line no. 03 will be called due to polymorphism .
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ananth Chellathurai wrote:There were 2 reasons.
1. You were trying to cast a String to Fractal object.
2. Return type of the getOb() method is Object. Object class does not have getMsg method in it.



Indeed there are several errors here, but I'm not agree with the interpretation above.

1. A call to getOb() method must perform first from left to right and to completion before doing a cast, but in this case no need for a cast since it return a String type.
2. Return type is a Fractal object not Object so, there is nothing to do with Object does not have getMsg() method.





Let's break the code down.



Hope that clear up your confusion...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Delson wrote:
2. Return type is a Fractal object not Object so, there is nothing to do with Object does not have getMsg() method.

Only in SubFractal. In Fractal it's defined as returning an Object (it's using overriding with a covariant return value). The reference type is of Fractal, so ft.getObj() is interpreted as returning an Object. So a cast is necessary.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Tommy Delson wrote:
2. Return type is a Fractal object not Object so, there is nothing to do with Object does not have getMsg() method.

Only in SubFractal. In Fractal it's defined as returning an Object (it's using overriding with a covariant return value). The reference type is of Fractal, so ft.getObj() is interpreted as returning an Object. So a cast is necessary.



That's incorrect and yes Fractal defined return an Object on getOb() method, but in this case it is not Covariant return scenario here. Look carefully there is no covariant return.

getOb() method in Fractal defined Object as a return type simply Object is super class of all class, and it return Fractal instance. There is no overriding method here either...

This is incorrect "so ft.getObj() is interpreted as returning an Object. ", this call polymorphism, at run time the actual object "ft" refer to is SubFractal not Fractal so, the getObj() method in SubFractal is called. Reread Chapter 2 in K & B book on Polymorphism for more insight...


Recap from Chapter 2 page 153 on Table 2-3 in K & B book:

Invocation on Overridden Method
"Object type (in other words, the type of the actual instance on the heap) determines which method is selected. Happens at runtime.









This is how Covariant return should be:




Here is a quick recap from Chapter 2 page 165 in K & B book:

Overriding and Return Types, and Covariant Returns
When a subclass wants to change the method implementation of an inherited
method (an override), the subclass must define a method that matches the inherited
version exactly. Or, as of Java 5, you're allowed to change the return type in the
overriding method as long as the new return type is a subtype of the declared return
type of the overridden (superclass) method.

Let's look at a covariant return in action:






}
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Delson wrote:That's incorrect and yes Fractal defined return an Object on getOb() method, but in this case it is not Covariant return scenario here. Look carefully there is no covariant return.

It's overriding with a different return type (which is a subtype of the original return type). That's the very definition of a covariant return. Your example replaces a return type of Fractal with SubFractal. The original replaces a return type of Object with Fractal. That's exactly the same principle in action.

I stand by my original statement
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Delson wrote:
This is incorrect "so ft.getObj() is interpreted as returning an Object. ", this call polymorphism, at run time the actual object "ft" refer to is SubFractal not Fractal so, the getObj() method in SubFractal is called. Reread Chapter 2 in K & B book on Polymorphism for more insight...


As for this one, polymorphism cannot determine the return type. That is determined from the return type of the method that exists in the class of the reference variable. Polymorphism determines which method gets called, but a run-time decision cannot be used for compile-time type safety.

If you were correct, the following would compile:

It doesn't (feel free to try it yourself). But the following does:
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a tricked one if we don't pay close attention to the code. I've tested the code and realized
that if we use the cast to Fractal to call getMsg() method we're calling Fractal's getMsg() method.

However, if we use ref variable "ft" and call getMsg() method, at run time the actual object that's calling
getMsg() method is SubFractal. This is where Polymorphism is kick in...


See below for the tested codes example:



Let's break it down:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Delson wrote:[color=darkblue]This is a tricked one if we don't pay close attention to the code. I've tested the code and realized
that if we use the cast to Fractal to call getMsg() method we're calling Fractal's getMsg() method.


No, it doesn't call Fractal's getMsg() method. You were right first time - polymorphism happens. However, the compiler will use Fractal's getMsg() method to enforce compile-time type safety, as the polymorphism will only happen at run-time.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I was right at first, but because of the casting later I mixed up. I've tested the code to check does Polymorphism happen or not.

Below is a fully tested code that I've tested, you can run it and find out. Only through coding and testing can confirm and clear the doubt.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic