What is the output of the following program?

public class Greek {

     int i = 1;

     public int getI() {
          System.out.print("Super");
          return i;
     }

     public static void main(String arhs[]) {
          Greek ga = new Arabik();
          System.out.print(ga.i + " " + ga.getI());
     }
}


class Arabik extends Greek {

     int i = 2;

     public int getI() {
          System.out.print("Sub");
          return i;
     }

}
Explanation
First of all ga.i + " " + ga.getI() in System.out.print(ga.i + " " + ga.getI()); will be evaluated. Then the result will be printed out to console. There will not be a sequence of outputs of expressions: ga.i, then ga.getI(). First step is evaluation of ga.i (value is 1), second - ga.getI() method (it will print out Sub and return 2). Finally, System.out.print(ga.i + " " + ga.getI()); will print out 1 2.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes