• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding question

 
Ranch Hand
Posts: 76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question is from examlab

why it is giving compilation fail. please give me a detail explanation

thanks in advance
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can't cast from String to Fractal:



This works:



Nothing to do with overriding.

cheers
Bob
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TellTheDetails
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why don't you try to compile this code! The compiler will complain in line "String s=(Fractal)ft.getOb().getMsg(); " that :
_ Type mismatch: cannot convert from Fractal to String ( not in the same hierarchical tree )
_ The method getMsg() is undefined for the type Object
To make compiler happy, you must do st like this String s=((Fractal)ft.getOb()).getMsg();
You should comment at everywhere you can, to track the way it goes. Be sure about the overriding. Good luck!!!
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yen hoang wrote:why don't you try to compile this code! The compiler will complain in line "String s=(Fractal)ft.getOb().getMsg(); " that :
_ Type mismatch: cannot convert from Fractal to String ( not in the same hierarchical tree )
_ The method getMsg() is undefined for the type Object
To make compiler happy, you must do st like this String s=((Fractal)ft.getOb()).getMsg();
You should comment at everywhere you can, to track the way it goes. Be sure about the overriding. Good luck!!!



This was a tricky question and I didn't understand the answer until now.
Thanks
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I believe is the getOb() method of SubFractal is overriding the getOb() method of Fractal class (covariant return types), then why is it that invoking the getOb() method from ft object aways invokes the getOb() of Fractal and not SubFractal.

Please explain.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As bob explained (3 months ago) the cast will try to cast the result of the last method call (i.e. getMsg) into Fractal (but getMsg method's return type is String). That's why you are getting a compile time error. This is because the precedence of method call is more than type cast. That's why you need extra parenthesis to make it work (again explained by Bob)...
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

I got that thanks however I want to know one more thing that if I say the following statement which getOb() method will be called the superclass one or the subclass one.

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The getObj method of SubFractal class will be called (as ft refers to an instance of SubFractal), but since the type of ft is Fractal, so at compile time the call will be matched to getObj in Fractal class (which returns Object) so you'll have to use a cast for compilation to succeed.

 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know that member access operator has higher precedence than the casting operator. The explanation of compiler’s behavior is as follows –

At the current state –
1- compiler sees that after possible execution of first member access operator ft.getOb() would return object (java.lang.Object) type. Compiler knows that object class does not have a function getMsg() so would give compilation error as “The method getMsg() is undefined for the type Object”
2- Compiler sees that this expression has member access operator and type cast operator. As the member access operator has higher precedence, type cast would be done at the end and then it would try to assign Fractal value to an String which would be incorrect so it would give another compilation error as “The method getMsg() is undefined for the type Object”

I hope this solves all the confusion of compilers behavior about this program.

Regards
Salil Verma
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit. I got that.
 
Ranch Hand
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...although this is a very old post...but can any one please tell me why i can't do the following in Fractal class:


It is showing the following ERROR if i do so:


Is this just because they are two different objects and their return types are not subtype of each other?
But getMsg() type is same for both right?

Currently it is printing "SuperStar"....what i have to do if would like to print "SubStar"?


Thanks
 
Faisal Fuad
Ranch Hand
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the solution....it was my bad. I was confusing myself with Object Type and Reference Type and how the Compiler and JVM do behave accordingly with each one of them
Below code changes will print "SubStar" String as final result.

Many many thanks for all the critical explanations related to this topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic