• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Mock exam doubt

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

source: http://www.danchisholm.net/oct1/mybook/chapter11/exam1.html





does instance variables and instance methods have difference in overriding?

why the output's are different?

Thanks in Advance!
regards
samura

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instance variables can't override while instance methods can.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi



I am unable to understand the output.

((c20)this).m1() should call c's m1 right.
We have a D20 object which is casted into its supertype c20.
Why its not calling m1() on c20

A little clarification pls..


Thanks
Praveen SP

strange long line edited. Bu.
[ August 20, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Overriding only concerns instance methods. It has nothing to do with static methods or instance variables.
When writing ((C18)this).s1 in D18, polymorphism doesn't worh here. It just allows you to access the "C18 part" of D18 with the cast.

However, when writting ((C20)this).m1(); polymorphism works and the JVM pick the m1 method according to the object type and not the reference type. In that case, the object type referenced by this is D20 so the JVM choose the m1 method from D20. The cast has no effect in that case.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For variables, there is no operator overloading and overiding in java. based on refference variable only it will invoke.

For methods, we have mehod overloading and riding. when we are invoking a method the jvm will do the following things
1)If it is overrridden method, then it will invoke the based on runtime object but not refference type.
2)If it is not overridden method , then it will invoke based on refference type.
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I thank everyone...


regards
samura
 
This tiny ad is wafer thin:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic