can you create interface instance ?

Answers were Sorted based on User's Feedback



can you create interface instance ?..

Answer / balibalo

to the writer of #29:

what you propose doesn't work because at line where you have t.wish(), Java tries to find the wish() method in the Object class (since t is of type Object). As it doesn't exist in that class, your program fails to compile.
Simple.

Uday version compiles because there is not type unconsistency, but it still is an anonymous class call, hence not a correct answer to the root question (answer being already given: NO, you cannot create an instance of an interface but you can create references of it).

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / ram

hello uday, ur program is not working

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / mallesh

hey uday that is not creating intance for Test interface ,
ur implementing the interface using anonymous class
(unknoun class)

public class Test2 {

public static void main(String[] args) {
Test2 t=new Test2();
Runnable r=new Runnable() {
public void run() {
System.out.println("Hi");

}
}; r.run();
System.out.println(r.getClass().getName().toString());
System.out.println(t.getClass().getName().toString());

}
}

Output:Hi
Test2$1
Test2

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / naresh jangili

no we can't create an object to interface and also abstract because two classes are un implememnted class for every un implemented classes we can create only reference varable.
but for every implemented classes we can create objects.
eg: interface A{
public void test()
}
class B implementes A
{
public void test()
{
s.o.p("hello");
}
public static void main(String argss[])
{
B b=new B(); (object creation)
A a1=null; (reference variable)

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / abhishek pnadey

you cannot create an instance for interface
in the uday answer ..
he has not write semicolon after the new Test();

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / arnold schwarzenegger

Yes we can create an instance of Interface but not directly but by using its subclass. Just check the below code


interface check
{
public void method1();
}
abstract public class B implements check
{

public static void main(String[] args)
{
check c = new check()
{
public void method1()
{
System.out.println("interface B m2");
}
};
c.method1();
}
}

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / joginder singh

uday,,,dude where have u used or created an instance of
interface test,,,what u are doing is using a anonymous
inner class and den calling the method using instance of
that class....
dat wasnt d ques...

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / akthar

No,we can't create an object to an interface...as told uday he create a anonymous class same as Test interface ..if we remove the interface and method it is working ....so t doesnot act as object to an interface it acts as an anonymous class object...so it invoking its anonymous method..

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();
}
}

Is This Answer Correct ?    0 Yes 0 No

can you create interface instance ?..

Answer / yousif mustafa

I think UDAY is right and he gave the right answer.
We actually couldn't instantiate the interface but we could
instantiate object from class implements this interface and
override all of its methods due to interfaces could only
have abstract methods, but also we can use anonymous inner
class to do that and must override all the abstract method
within creating the object. for ex:

interface Test {
void printA();
void printB();
}

class Inter implements Test {
@Override
void printA() {System.out.println("A from implement");}
void printB() {System.out.println("B from implement");}
}

public class A {
public static void main (String arg[]) {
Test t = new Test() {
void printA() { System.out.println("A form inner"); }
void printB() { System.out.println("B from inner"); }
};
Inter i = new Inter();
t.printA();
i.printA();
t.printB();
i.printB();
}
}

Is This Answer Correct ?    0 Yes 1 No

can you create interface instance ?..

Answer / saroj singh

we can create instance of Interface like..


interface Imployee
{
void Pay();
void Salary();
}

class SeniorSoftEngg : Imployee
{
public void Pay()
{
Console.WriteLine("Inside Pay");
}
public void Salary()
{
Console.WriteLine("Inside Pay");
}
}
class Program
{
static void Main(string[] args)
{
Imployee Emp = new SeniorSoftEngg();
Emp.Pay();
Emp.Salary();

}

}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More Core Java Interview Questions

Is java se open source?

0 Answers  


Is char a method in java?

0 Answers  


How HashMap implemented in java? how it internally works when values are added or searched from hashMap?What is the difference betweenthe implementation of hashmap and Linked Hashmap?

3 Answers   IBM,


Explain numeric promotion?

0 Answers  


what is Vector class?

2 Answers  


What is the mapping mechanism used by java to identify IDL language?

0 Answers  


Mention some interfaces implemented by linked list in java.

0 Answers  


What is object in java?

0 Answers  


What are the steps that are followed when two computers connect through tcp?

0 Answers  


Explain the scope or life time of class variables or static variables?

0 Answers  


What is the difference between static (class) method and instance method?

0 Answers  


What is a stream? what are the different types and classes of Streams?

2 Answers  


Categories