MAQ Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
Works fine.
interface A
{
void m();
}
interface B
{
void m();
}
class Test implements A,B
{
public void m()
{
System.out.println("Done");
}
public static void main(String[] args)
{
Test t=new Test();
t.m();
}
}
if we want to use any particular method we need to give the fully qualified name of the interface
interface A
{
void m();
}
interface B
{
void m();
}
class Test implements A,B
{
public void A.m()
{
System.out.println("Done");
}
public void B.m()
{
System.out.println(" ok Done");
}
public static void main(String[] args)
{
Test ob=new Test();
A.m ao=ob;
B.m bo=ob;
ao.m();
bo.m();
}
}
Have you tried compiling this code? It don't think it will go through.
I have not seen anything like 'public void A.m()' so far in Java, let me know if I am missing anything here.
At least in C#, it will compile and run just fine.
When implementing an interface, you need to comply with the method headers that are defined in the interfaces. If you implement 2 interfaces, then as long as you implement the methods (headers can be the same), then you're just fine.
For instance, the following will compile and run just fine:
class Program
{
static void Main(string[] args)
{
C c = new C();
c.f(); //this will run just fine, and type bbbb
}
}
interface A
{
void f();
}
interface B
{
void f();
}
class C : A, B
{
public void f()
{
Console.WriteLine("bbbb");
}
}
public interface A {
void m();
}
public interface B {
void m();
}
public class mainTest {
public static void main(String []args)
{
Test t = new Test();
t.m();
}
}
public class Test implements A,B {
@Override
public void m() {
// TODO Auto-generated method stub
System.out.println("University Of Delhi , India");
}
}
This is what I found from Oracle Website-
- Mohsin Tejani October 10, 2012"If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile."
Answer to your question:
Class C will compile and run successfully. No error it works fine
As method max() has same signature in both Interfaces, we'll treat it as a single abstract method and define it only once.
Compiler will check if you implemented the methods declared in Interfaces.
As per the contract you did.
There is no question of Run time error as Interface doesn't have definition of method max()