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


did interface can implementation method ? i know its not
possible but my interviewer said its possible ..but how..?
any one have idea ???

Answers were Sorted based on User's Feedback



did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / arpan mondal

Yes, an interface can if it has a class in it.

Here is the code what i am trying to say...

// MethodImplementedInterface.java
/**
*@author Arpan Mondal(www.arpanmondal.tk)
*/

interface A {
public class B {
B() {
display();
}
public void display() {
System.out.println("This is the
method in the interface.");
}
}
}

class C implements A {
B objB = new B();
void display() {
System.out.println("This is the method in
the class.");
}
}
public class MethodImplementedInterface {
public static void main(String[] args) {
C objC = new C();
objC.display();
}

}

The output of the above code is here...

This is the method in the interface.
This is the method in the class.

Is This Answer Correct ?    10 Yes 0 No

did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / suhail

Interface can i have implementation for method by using
class inside the interface this is the only way to have the
implementation...

public interface A{
public class B{
public void m1(){
}
}
}

Is This Answer Correct ?    9 Yes 1 No

did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / keerthi

method implementation is impossible in interface , method
declaration only possible in interface.

In abstract class method declaration or implementation
possile.

If we implement methods in iterface what is purpose of
abstract concepts.

The interviewer asked for checking whether your confident
or not

Is This Answer Correct ?    7 Yes 1 No

did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / lakshmikanth

We can implement the method in the interface anonymously.

for example :

public class TestInterface {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A o1= new A(){
public void f()
{
System.out.println("testing ....");
}
};
o1.f();
}

}
interface A
{
void f();
}

please let me know our concerns .

Lakshmiakanth.

Is This Answer Correct ?    3 Yes 0 No

did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / vinod

Hi Lakshmikant,

by writing anonymous class you are actully implementing an interface

It does not mean that we can have implementation with in an interface.

Is This Answer Correct ?    2 Yes 2 No

did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / prabhu prasad

Hi Laxmidhar,

In your program you declare a reference variable, o1, of type interface A. Then you declare a new class that
has no name (means a anonymous class). Though A is an interface so the anonymous class actually implementing interface A…

If A is a class instead of interface then the anonymous class would be the subclass of A..

Is This Answer Correct ?    0 Yes 0 No

did interface can implementation method ? i know its not possible but my interviewer said its possi..

Answer / amit singh

its a not correct
true answer is that
if the interviewer ask that there is any backdoor
to do this then it is correct but "I THINK INTERVIEWER HAD
SOME HANGOVER"
right answer is no
above coding is right but not the answwer
beacuse
1):what he did in this code just define a class inside a
interface and define a method in this class
so question is not solve there is question in a question
and the answer is "YOU JUST BINDING THE B type with this
INTERFACE A"
not method implementation
amitsing2008@gmail.com

Is This Answer Correct ?    0 Yes 1 No

Post New Answer



More Core Java Interview Questions

How can we achieve IPC in JAVA?

1 Answers   Wipro,


What is substring of a string?

0 Answers  


What is formatted output?

0 Answers  


How does predicate work in java?

0 Answers  


What are the different types of garbage collectors in java?

0 Answers  






11. static class A { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B”); } 16. } 17. public static void main(String[] args) { 18. new B().process(); 19. } What is the result? 1 B 2 The code runs with no output. 3 Compilation fails because of an error in line 12. 4 Compilation fails because of an error in line 15.

6 Answers  


What is sorting in java?

0 Answers  


What is the purpose of using break in each case of switch statement?

0 Answers  


Is java platform independent?

0 Answers  


Is it possible to override the main method?

0 Answers  


What collections you have worked on? Internal working of Hashmap?

0 Answers   Bravura Solutions,


Is string is a keyword in java?

0 Answers  






Categories