can you create interface instance ?

Answers were Sorted based on User's Feedback



can you create interface instance ?..

Answer / john

@Uday Ur fooling amateur coders by creating an anonymous
class within the class . U cannot create an instance of an
Interface .If so try to make execute this code lol class

interface Test{
public void wish();
}
class Main
{
public static void main(String[] args)
{
Test t=new Test();
t.wish();
}
public void wish()
{
System.out.println("output: hello how r u");
}
}

Is This Answer Correct ?    12 Yes 3 No

can you create interface instance ?..

Answer / shanmukha

Hi,
We can't create instance for an interface or abstract
class.Just remove the following code from the program
written by Uday.
interface Test
{
public void wish();
}
Remove the above code and run the program,then program will
compile and execute properly,because he used anonymous class.

Is This Answer Correct ?    11 Yes 2 No

can you create interface instance ?..

Answer / sushma

hi nishant,
whatever the explanation u have given is exactly right. we cant create an instance for interface. the instance created is for annonymous inner class and not of interface Test.

Is This Answer Correct ?    3 Yes 0 No

can you create interface instance ?..

Answer / tathagata

hi uday
I also tried this to call a interface of Collection and try
to instantiated but it is not working.
import java.util.*;
interface Test
{
public void wish();
}
class a implements Test
{
public void wish()
{
System.out.println("I am fine");
}
}
class Main
{
public static void main(String[] args)
{
Collection c =new Collection()
{
boolean isEmpty()
{
return true;
}
};
System.out.println(c.isEmpty());
Test t=new Test()
{
public void wish()
{
System.out.println("output:
hello how r u");
}
};
if(t instanceof Test)
{
System.out.println("t is a instance
of Test");
}
t.wish();
a oba=new a();
oba.wish();
}
}


But it is not working please explain.

Is This Answer Correct ?    2 Yes 0 No

can you create interface instance ?..

Answer / ravikiran

No we can create an instance of a class which implements
this interface.

Is This Answer Correct ?    2 Yes 0 No

can you create interface instance ?..

Answer / anthony

cool buddy. Nice way to fool people.

If you really need to check the concept, put the code in
eclipse or netbeans and see. The method in the anonymous
class would show as overriden from interface Test and if
you remove the method from the anonymous class, it would
not compile. So at the end you are having an instance of
class implementing the interface.

Is This Answer Correct ?    2 Yes 0 No

can you create interface instance ?..

Answer / rasmitha

No,But we can Create reference for interface.
For Example.

java.sql.Connection is an interface,But in the
Establishmentof the Database Connetion.

Connection con = DriverManager.getConnetion();

con is an ref Varieble that holds Connetion Object yar..

Is This Answer Correct ?    2 Yes 0 No

can you create interface instance ?..

Answer / venkat

No, we can't instantiate the interface

Is This Answer Correct ?    2 Yes 0 No

can you create interface instance ?..

Answer / cps

@ Uday... nice way to fool around...


Check <
http://java.sun.com/docs/books/tutorial/java/IandI/interfaceAsType.html
>

Is This Answer Correct ?    1 Yes 0 No

can you create interface instance ?..

Answer / bibhuti

i am bit confused with this one.
If it is referring to an anonymous class then if i am changing like below then why it is not working?

public static void main(String[] args) {
Object t = new TestOne() {
public void wish() {
System.out.println("output: hello how r u");
}
};
t.wish();
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

How does queue work in java?

0 Answers  


We have two methods to create methods the threads. 1. Implementing runnable interface 2. Extending to thread class and overriding run method. Among these two which one is better and why? Please explain me in detail.

2 Answers  


How use .contains in java?

0 Answers  


What is quick sort in java?

0 Answers  


What is array sorting in java?

0 Answers  


How does callback work in java?

0 Answers  


What are the rules for naming an array?

0 Answers  


what is the main difference between string and stringbuffer? can you explain it with program?

2 Answers  


In a program, initializing an array of 100 KB is throwing an out of memory exception while there is 100 MB of memory available. Why?

0 Answers   MCN Solutions,


write a class to input 2 points by the user & check the line maked by the points is horizontal,vertical or rectangle?

1 Answers  


Is java 11 paid version?

0 Answers  


Explain how to force the garbage collection in java.

0 Answers  


Categories