Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

how does java implement all this ?

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

It prints 0 as expected. now if i change void print() at line1 to private void print() or static void print().. it prints A200 as expected since method now cannot be overloaded.
how does java implement all this ?

[ March 27, 2005: Message edited by: Aruna Agrawal ]
[ March 27, 2005: Message edited by: Aruna Agrawal ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did it print 0?
The print line in the extended class is commented thereforte, will not call the print method. Or am I missing something?

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

it prints A200 as expected since method now cannot be overloaded.



well it's not overloading ....it's overriding

i think JVM at run-time is taking the most-specific method here....
not sure ......all out there plz verify this statement

thanx
amit
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the sequences happening in the code above, assuming a.print() is uncommented
  • Creating object of class A100, this will invoke the constructor of parent class A200
  • A200 ctor calls print method of A100 (polymorphism overriding), at this point of time int i is zero. So that will print 0.
  • When the construction of object a is complete, a.print() again makes a polymorphic call and calls print method of A100. However at this point of time, i results to 4 because of Math.round(3.5f)
  • So the output is 0 4.


  • Hope that helps...
    [ March 27, 2005: Message edited by: Jay Pawar ]
     
    Amit Das
    Ranch Hand
    Posts: 206
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hey Jay,

    whatever you've mentoned is i feel already know by Aruna also, i think she's asking for the explanation that in the superclass if we define print() as private the o/p changes......how???

    i as per my understanding of the question repiled to this saying that JVM takes the most-specific method here, but i'm not sure that most-specific oncept can be applied here......
     
    Aruna Agrawal
    Ranch Hand
    Posts: 66
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey Jay,

    I and Amit know what happens.. thats what u also mentioned ..
    we want to know how it happens.. metaknowledge of what happens..

    Aruna
     
    Jay Pawar
    Ranch Hand
    Posts: 411
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry guys for previous confusion...

    [Aruna]: now if i change void print() at line1 to private void print()

    As you might be aware that if the print method is private in the parent class A200, then it is not visible to the derived class A100. So there is no question of overriding in this case. The only print method the ctor of class A200 is aware is it's own print method. Hence you get the output as A200.


    [Aruna]: now if i change void print() at line1 to static void print()..

    This won't compile. Static methods cannot be overridden. However you can hide them in the derived class.

    Hope I tried to be as clear as possible this time.
     
    Amit Das
    Ranch Hand
    Posts: 206
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi aruna,

    i think you are writing SCJP tommorrow....right??? even if not...
    Best of Luck!!!
     
    Aruna Agrawal
    Ranch Hand
    Posts: 66
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes day after tomm .. thanks
     
    This looks like a job for .... legal tender! It says so right in this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic