OCJP Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gautamz07
    New Member
    • Sep 2013
    • 26

    OCJP Question

    Code:
    interface TestA {String toString(); }
    public class Test{
    public static void main(String[] args) {
    System.out.println(new TestA()) {
    public String toString() {return "test"; }
    });
    }
    }

    What is the result ?
    1. Test
    2.Null
    3. An exception thrown at runtime.
    4. Compilation fails because of an error in line 1.

    I Think it should be 3 But can someone explain whats the right answer and why ? :)
    Last edited by Niheel; Nov 21 '13, 06:53 PM.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Why do you think it is (3)?
    You can easily try it out by running it and watch the result.

    If we give you the answer with explanation, then you would not learn. But you are doing the OCJP because you WANT to learn. We can support you in learning by telling you if your reasons are valid and maybe give you a hint.

    Comment

    • gautamz07
      New Member
      • Sep 2013
      • 26

      #3
      Because i think you cannot creat an instance of an interface .

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        There is a syntax error in your code (an extra ")" in line 4) but other than that it should compile. To understand why, read up on anonymous classes.

        Comment

        • itsraghz
          New Member
          • Mar 2007
          • 124

          #5
          Hi gautam,

          Though at first site, you may sound to be correct, it is NOT true. Yes, interfaces can not be instantiated directly as below

          Code:
          interface MyInterface{}
          
          class MyClass
          {
             public void myMethod()
             {
                MyInterface myIntObj = new MyInterface();
             }
          }
          The above codes throws a compiler exception saying that an interface can't be instantiated (more precisely, using a new operator).

          Code:
          MyInterface is abstract; cannot be instantiated
          However, what the code is doing is supplying an anonymous class which is implementing that interface. That is very valid and legal in Java.

          FYI, I have added a small line of code (line #13) to test the implementing object is an instance-of Interface or not (using instance-of). I have also made the necessary indentation for an easy read.

          Code:
          interface TestA 
          {
          	String toString(); 
          }
          public class Test
          {
          	public static void main(String[] args) 
          	{
          		System.out.println(new TestA() {
          		
          			public String toString() 
          			{
          				System.out.println("instanceoftest -> "+ (this instanceof TestA));
          				return "test"; 
          			}
          		});
          	}
          }
          The output is as follows

          Code:
          instanceoftest -> true
          test
          Hope this clarifies and helps.

          Comment

          Working...