473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

OCJP Question

gautamz07
Expand|Select|Wrap|Line Numbers
  1. interface TestA {String toString(); }
  2. public class Test{
  3. public static void main(String[] args) {
  4. System.out.println(new TestA()) {
  5. public String toString() {return "test"; }
  6. });
  7. }
  8. }
  9.  

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 ? :)
Nov 21 '13 #1
4 1938
chaarmann
785 Expert 512MB
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.
Nov 22 '13 #2
Because i think you cannot creat an instance of an interface .
Nov 22 '13 #3
Nepomuk
3,112 Expert 2GB
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.
Nov 22 '13 #4
itsraghz
127 100+
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

Expand|Select|Wrap|Line Numbers
  1. interface MyInterface{}
  2.  
  3. class MyClass
  4. {
  5.    public void myMethod()
  6.    {
  7.       MyInterface myIntObj = new MyInterface();
  8.    }
  9. }
  10.  
The above codes throws a compiler exception saying that an interface can't be instantiated (more precisely, using a new operator).

Expand|Select|Wrap|Line Numbers
  1. MyInterface is abstract; cannot be instantiated
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. interface TestA 
  2. {
  3.     String toString(); 
  4. }
  5. public class Test
  6. {
  7.     public static void main(String[] args) 
  8.     {
  9.         System.out.println(new TestA() {
  10.  
  11.             public String toString() 
  12.             {
  13.                 System.out.println("instanceoftest -> "+ (this instanceof TestA));
  14.                 return "test"; 
  15.             }
  16.         });
  17.     }
  18. }
  19.  
The output is as follows

Expand|Select|Wrap|Line Numbers
  1. instanceoftest -> true
  2. test
  3.  
Hope this clarifies and helps.
Nov 26 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
6
semanticnotion
by: semanticnotion | last post by:
Hi sir i want to transform the data of one table into another through foreign key but the following error come to my browser Here is my code and data base structure. CREATE TABLE IF NOT...
10
by: fadhili | last post by:
<?php /*the points system for this Q&A has to be passed from question to question. We are going to do this by passing it in the URL. After the first question, the current score is passed...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.