• 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

print wrong output in extended class (Enthuware)

 
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I am really stuck in this one.....

I bumped into this on Enthuware Standard Test 1
I cannot undestand this at all..
So I know that when applying polymorphism, the methods are from the subclass and the variables from the superclass.



So far so good.
I understand everything until this point.

Then this happens:


and prints out:



Why???
      Shouldn't it output:

???

Why does the SubClass 5 go first???      





 
Ioanna Katsanou
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me I meant to type Polymorphism in the header of the post.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:
Then this happens:


and prints out:



Why???
     



You do know that this topic has nothing to do with polymorphism right?

Simply. The println() method in this example is taking *one* parameter. And the parameter is the resultant of the expression. In other words, the Java code has to do all the evaluations and concatenations needed to calculate the resultant *before* passing it to the method. So, if the calculations has side effects, then obviously, it should also happen before calling the println() method.

Henry
 
Ioanna Katsanou
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

When I posted this I didn't understand exactly what I was asking.
I now understand that this has to do with the println method. Is it possible to change the subject of the topic to something more relevant?

I apologize for my ignorance.

Ioanna
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:Why does the SubClass 5 go first?


This code snippet (and its output) is already explained in great detail in this topic. So I'm pretty sure when you have carefully read this excellent topic you'll have a perfect understanding why this output is printed. If you still have doubts and/or questions, just let us know by replying to this topic.

Hope it helps!
Kind regards,
Roel
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic