It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why compilation of the code fails here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why compilation of the code fails here" Watch "Why compilation of the code fails here" New topic
Author

Why compilation of the code fails here

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

Why don't you tell us what error message you get? And where it says the error is?
Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
At line number 16 it showing 2 errors .
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Problem is with this line,


try the following code,


Ananth Chellathurai [Walk on software]
Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
Yes Ananth its working.. What was the reason of error ?
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

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

Joined: Oct 02, 2003
Posts: 11175
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
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

Joined: Jul 15, 2010
Posts: 102
Ananth i think getOb() at line no. 03 will be called due to polymorphism .
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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...

OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Apr 13, 2011
Posts: 206
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Apr 13, 2011
Posts: 206
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Apr 13, 2011
Posts: 206
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.


 
 
subject: Why compilation of the code fails here
 
Similar Threads
overriding question
Lets add some good scjp 5 questions here
problem