Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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

What is the use of keywords in java?

0 Answers  


What is a condition in programming?

0 Answers  


In which JDK version event-delegation model is introduced?

1 Answers  


23. Storage space in java is of the form Stack Queue Heap List 24. What is java code embedded in a web page known as Applets Servlets scriptlets snippets 25. Which of the following attributes are compulsory with an <applet> tag?. code,height & width. 26. What does 'CODEBASE' in an applet tag specify?. Files absolute path.

0 Answers   TCS,


Is &&= a valid Java operator?

2 Answers  






What do you mean by platform independence?

0 Answers  


How does multithreading take place on a computer with a single cpu?

0 Answers  


Which java collection does not allow null?

0 Answers  


What are some characteristics of interference class?

0 Answers  


Can you change array size in java?

0 Answers  


Can you pass by reference in java?

0 Answers  


Can we clone singleton object?

0 Answers  






Categories