can you create interface instance ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What is the difference between ArrayList and Vector? which one is better in Java
0 Answers SkillGun Technologies,
What is json parser in java?
How many digits is int32?
What do you mean by Hash Map and Hash Table?
Explain the protected field modifier?
What are the two types of streams offered by java 8?
Hi, This is ravi i have a question like this i have string "UNDERSTAND" now i want to count the letters how many times it occures.i.e from the above string the out put should be like this U-1,N-2,D-2,E-1,R-1,S-1,T-1,A-1. how can i achieve this Thnaks in advance for your response ..
What is concurrent hashmap and its features?
why an outer class cannot be declared as private?
What is the main difference between java platform and other platforms?
How does java pattern compile work?
What is consumer in java?