Sunday, August 11, 2019

What is the output of the following code?

Problem statement: 
Given the code of java. Can you tell me the output of this?
  1. interface Annonymous {
  2.     public int getValue();
  3. }
  4. public class Sample1 {
  5.     int data = 15;
  6.     public static void main(String[] args) {
  7.         Annonymous a = new Annonymous() {
  8.             int data = 5;
  9.             public int getValue() {
  10.                 return data;
  11.             }
  12.             public int getData() {
  13.                 return data;
  14.             }
  15.         };
  16.         Sample1 s = new Sample1();
  17.         System.out.println(a.getValue() + a.getData() + s.data);
  18.     }
  19. }
Output:
compilation error at line number 17 due to a.getData(). Here getData() is not a method of Annonymous interface.

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...