• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Unable to interpret the code

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friend ,
The output this is " sub 1 2" and not able to understand how

here's the code


According to me this output must be " 1 sub 2"

I would be very grateful if you could help me out sort this one.
 
Greenhorn
Posts: 13
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi divyya, first the getI() method is called, and after that SOP runs.

Step by step , its like this :

1. ga.getI() is evaluated... so "Sub" prints.

2. sop runs as (1 + " " + 2) , ie, "1 2" gets printed.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhinav can you please give me reason why ga.getI() will run ?
 
Abhinav Shukla
Greenhorn
Posts: 13
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i assume you mean to ask why ga.getI() will run "first"

not sure..
but its the way JVM works.. maybe they might have mentioned this in Java Language Specification.

hopefully someone else might have a different / better answer.
 
Ranch Hand
Posts: 185
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't know, but println() is a method and you call it with a String argument which is built from two ints and one String. In order to get the second int the method getI() has to run and return the int. When the method code of println() starts running the argument has to be already there, so getI() has already run and 'sub' is already printed.

Not sure about this,
John
 
Ranch Hand
Posts: 31
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, to run the println method it has to run the getI() method within it first, and to run getI() it has to print something. So it will do the println() in getI() before it prints the output in main.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for your reply.
 
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why doesn't it print "Super" then?
Can anybody please write the line number it goes to step by step?
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't print super because the getI function in Arab overrides the one in Greek. The instance variables aren't overridden though, that's just the way it works. So the order the lines execute are 9 -12, but to execute line 12 it has to do 18-22 before it finishes, i think.
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To evaluate an expression, the operands are evaluated 1st. So, when the expression ga.i+" "+ga.getI() is to be evaluated, the method invocation is done 1st and since its a classic case of run-time polymorphism, the subclass method getI() is run printing "Sub" and returning 2.


The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, ||, and ? : ) appears to be fully evaluated before any part of the operation itself is performed.



The above quote is from the below link.

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html
 
Anjali Vaidya
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Benjamin! I had forgotten that instance variables aren't overrided
 
Ranch Hand
Posts: 78
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tony (The Iron Man) for the explaination. Thank you benjamin.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anjali Vaidya wrote:why doesn't it print "Super" then?
Can anybody please write the line number it goes to step by step?



The getI method isnt like a constructor that makes calls to its super class before returning the subclass.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic