can you create interface instance ?

Answers were Sorted based on User's Feedback



can you create interface instance ?..

Answer / kcs

No, We cannot create an instance for interface. But we can
create a reference for an interface.

Is This Answer Correct ?    75 Yes 17 No

can you create interface instance ?..

Answer / devir

No,we cannot create an instance for interface

Is This Answer Correct ?    74 Yes 26 No

can you create interface instance ?..

Answer / anil

That it is not creating the instance of the interface. The
class which is there on the right side is called anonymas
call.
It is correct when the statement is as below
Test t =new Test(); he should give the ';' after the
paranthesis not after the '}'.

Please refer anonymas classes for other information how
they works...

Is This Answer Correct ?    37 Yes 1 No

can you create interface instance ?..

Answer / arvind panwar

No, we can't create an instance of an interface but
reference variable can hold reference to object of its sub
class.

Is This Answer Correct ?    46 Yes 13 No

can you create interface instance ?..

Answer / deepesh

Hi Uday.

Actually, in your problem, a reference of anonymous
inherently assigned to interface. In program, you no where
wrong, but instance of interface were not created, it
simply assigning the reference of anonymous class to
interface.. That is possible.

any way.. We can't create instance of interface.. In that
example only reference assignment to interface by anonymous
class.
Hope everybody can understand.

Is This Answer Correct ?    30 Yes 1 No

can you create interface instance ?..

Answer / devir

No,Uday is wrong.
He has created an anonymous inner class Test with the same
name as interface Test.It doesn't depend on the interface Test.
If an instance for interface Test has to be created ,he
sholud have implement interface Test such as
class Main implements Test{
}

Is This Answer Correct ?    17 Yes 2 No

can you create interface instance ?..

Answer / nishant

hi Uday,what you've done is, created an annonymous inner
class which is the implementer class of the interface.Here
the refernce variable is of the interface but it is
referencing to the newly created annonymous inner class
instance.
Here the instanceof operator returns true because
annonymous inner class is the implementer of interface and
instanceof always returns true with the same class or its
super class(try using instanceof with Object class, it will
always return true,may the object be of any class).

Is This Answer Correct ?    14 Yes 0 No

can you create interface instance ?..

Answer / zafar

No, you cannot create an instance of an interface. An
interface has no implementation - a class that implements
the interface specifies the implementation.

However, you can ofcourse have a reference variable of an
interface type that points to an instance of a class that
implements the interface. For example:

// List is an interface, ArrayList implements interface List
List data = new ArrayList();


It's good practice to program like this - program to an
interface, not an implementation. If you want to know more
about that design principle, see, for example:
[url=http://www.artima.com/lejava/articles/designprinciples.
html]Design Principles from Design Patterns[/url]

Is This Answer Correct ?    13 Yes 0 No

can you create interface instance ?..

Answer / arun savoji

This expression instantiates a new object from an unnamed
(called anonymouns inner class)and previously undefined
class, which automatically implements the interface Test.
The class can explicitly implement one, and only one
interface, and cannot extend any class other than Object.

As it implement the Test interface so our anonymous inner
class has to implement the methods in this case we have to
implement wish() method.

In java we can not create instance of an interface, since
interface do not have contructor and as well as defalut
contructor.

Is This Answer Correct ?    9 Yes 0 No

can you create interface instance ?..

Answer / swati

we can't create instance of an interface..
but we can create reference of interface, which can refer
to upcoming object of the class who implements that
interface..

Is This Answer Correct ?    9 Yes 0 No

Post New Answer

More Core Java Interview Questions

How can we make a class virtual?

0 Answers   Fidelity,


what is difference between signed & unsigned char?

2 Answers  


explain the concept of inheritance with an example?

9 Answers   Polaris,


What is linked hashmap and its features?

0 Answers  


What are namespaces in java?

0 Answers  






define polymorphism in java

0 Answers  


difference between vectorlist and hash

1 Answers   TCS,


Can memory leak in java?

0 Answers  


what is use of marker interface? give me good example?

2 Answers  


what are the rules to use try catch finally?

1 Answers   Satyam, UJ,


Define Multiprogramming and Multiprocessing in java.

0 Answers   Akamai Technologies,


What is dynamic array in java?

0 Answers  


Categories