ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
can you create interface instance ?
 Question Submitted By :: Rajnish.bhaskar
I also faced this Question!!     Rank Answer Posted By  
 
  Re: can you create interface instance ?
Answer
# 1
sdfg
 
Is This Answer Correct ?    5 Yes 9 No
Guest
 
  Re: can you create interface instance ?
Answer
# 2
No,we cannot create an instance for interface
 
Is This Answer Correct ?    15 Yes 4 No
Devir
 
 
 
  Re: can you create interface instance ?
Answer
# 3
No, We cannot create an instance for interface. But we can 
create a reference for an interface.
 
Is This Answer Correct ?    12 Yes 0 No
Kcs
 
  Re: can you create interface instance ?
Answer
# 4
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 ?    9 Yes 0 No
Arvind Panwar
 
  Re: can you create interface instance ?
Answer
# 5
Why Not?
Yes, Even we can create an instance of interface and 
abstract class also.
just see this below simple example

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

cmd> javac Main.java
cmd> java Main
output: hello how r u

in above case Test is an interface but i created the object 
for that interface.
 
Is This Answer Correct ?    7 Yes 8 No
Udaykiran
 
  Re: can you create interface instance ?
Answer
# 6
Wov that was amazing Uday
 
Is This Answer Correct ?    0 Yes 4 No
Sankar
 
  Re: can you create interface instance ?
Answer
# 7
hey uday u r master in java yaar, by people like u we can 
change the java world keep it up dude. just keep on posting 
these kind of impossible things ok. Thanks a lot for that :)
 
Is This Answer Correct ?    1 Yes 3 No
Akhilesh
 
  Re: can you create interface instance ?
Answer
# 8
Uday..........Plz change ur name to James Gosling 
Jr......dat suits u
 
Is This Answer Correct ?    0 Yes 1 No
Anil
 
  Re: can you create interface instance ?
Answer
# 9
!!!!!!!!!!!!!!!!!!marvelous!!!!!!!!!!!!!
 
Is This Answer Correct ?    0 Yes 1 No
Prakash
 
  Re: can you create interface instance ?
Answer
# 10
hey amazing answer uday.
 
Is This Answer Correct ?    0 Yes 1 No
Ram
 
  Re: can you create interface instance ?
Answer
# 11
amazing
 
Is This Answer Correct ?    0 Yes 1 No
Manish Malav
 
  Re: can you create interface instance ?
Answer
# 12
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 ?    4 Yes 1 No
Anil
 
  Re: can you create interface instance ?
Answer
# 13
Hi Anil, In this case uday sounds good!
As per your consideration if it is an anonymous class then 
just remove that interface from our application and try to 
compile it won't compile that code.
Here Test is an interface, for which to create the object 
we need to provide the complete implementation for that 
interface the same thing which uday did. 
and again u check it if(t instanceof Test)
it is returning true that means t is an instance for Test.
and here the object is creating in the heap.
at finally here an instance is created for an interface.
 
Is This Answer Correct ?    1 Yes 2 No
Kishore Emani
 
  Re: can you create interface instance ?
Answer
# 14
Hi all,

I think Udaya is right and i have tested in the same and its
working. But if you think about the way JAVA has to make
instances(e.g. A a = new A()) will not work. It will give
you compilation error. So there is something we are not able
to see. Even I am trying to get what this example actually
working.

Feel free to update me if I am wrong...☺
 
Is This Answer Correct ?    1 Yes 1 No
Guest
 
  Re: can you create interface instance ?
Answer
# 15
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 ?    6 Yes 1 No
Deepesh
 
  Re: can you create interface instance ?
Answer
# 16
Hi Deepesh, uday is back again

Hey just see the question . can you create interface 
instance?
just say the answer Yes or No?
my answer is Yes
how is it possible? Already i gave the example.I know that 
this is anonymous inner class concept. But i don't want to 
exceed this concept for too complex. that's why i did it in 
this manner.Any How i thanks to all of you for more inputs.

but u can create the instance for interface. how  can we 
check an instance is,using instance of operator  only. here 
it proved hence the affrimative ans is right. i am not 
plmbbing your knowledge it's just a kind of inquiry. Thanks 
FYI huh!!!!
 
Is This Answer Correct ?    0 Yes 4 No
Uday
 
  Re: can you create interface instance ?
Answer
# 17
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 ?    0 Yes 0 No
Tathagata
 
  Re: can you create interface instance ?
Answer
# 18
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 ?    4 Yes 0 No
Devir
 
  Re: can you create interface instance ?
Answer
# 19
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 ?    2 Yes 0 No
Nishant
 
  Re: can you create interface instance ?
Answer
# 20
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 ?    3 Yes 0 No
Arun Savoji
 
  Re: can you create interface instance ?
Answer
# 21
No we can create an instance of a class which implements
this interface.
 
Is This Answer Correct ?    1 Yes 0 No
Ravikiran
 
  Re: can you create interface instance ?
Answer
# 22
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 ?    6 Yes 0 No
Zafar
 
  Re: can you create interface instance ?
Answer
# 23
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 ?    4 Yes 0 No
Swati
 
  Re: can you create interface instance ?
Answer
# 24
@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 ?    2 Yes 0 No
John
 
  Re: can you create interface instance ?
Answer
# 25
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 ?    2 Yes 0 No
Shanmukha
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
How do you set security in applets? Wipro1
Difference between Choice and a List?  1
how can we use the servlet as standalone apllication?should we need to extend any class? Logica-CMG2
What is Overriding and how can it be used? Wipro5
What is clipping?  2
What is HashTable?  3
Difference between the paint() and repaint() methods?  1
What is more advisable to create a thread, by implementing a Runnable interface or by extending Thread class?  5
How does serialization work Ordain-Solutions2
What are File and RandomAccessFile classes?  1
When will we use class loader?  1
Can we access a database using applets?  2
what is difference Between Core Java and advance java HCL19
what is the purpose of the final in the try-catch-final  5
Is ResultSet class? Bally-Technologies2
What are MalformedURLException and UnknownHost Exceptions and whey they will be thrown?  1
explain the classification of exception and hoew to handle the exceptions  2
which one the better thread emplemented thread or extended ? Fidelity3
What are the advantages of the model over the event- inheritance model?  1
1.what is the exact difference between applet and frame? 2.Do we use main method in frames?  2
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com