VMWare Inc Interview Question
Quality Assurance EngineersTeam: QATeam
Country: India
Interview Type: In-Person
It will belong to a particular interface based on its child class runtime polymorphism.
If the object instance is created using
IA ia = new TestUtils();
ia.print();
Then its belongs to A interface.
//IA.java
public interface IA {
public void print();
}
//IB.java
public interface IB {
public void print();
}
//TestUtils.java
public class TestUtils implements IA, IB {
@Override
public void print() {
System.out.println("print");
}
public static void main(String[] args) {
IA ia = new TestUtils();
IB ib = new TestUtils();
ia.print();// IA'a print method
ib.print();// IB's print method
}
}
It depends on the method on interface B and C. If interface B and C have the same exact method, then it would not be distinguishable. However, if the method for interface B and C is like this,
void someMethod(some parameter)
boolean someMethod(some parameter)
Then it depends what method class A is specifically looking for.
for example
- nagarjuna.lingala August 08, 2013public interface A {
public void show();
}
public interface B {
public void show();
}
public class Test implements A, B {
public static void main(String[] args) {
A a;
B b;
Test t = new Test();
a = t;
a.show();
b = t;
b.show();
}
@Override
public void show() {
System.out.println("this method is from class Test:");
System.out.println(this.getClass().getInterfaces()[0]);
System.out.println(this.getClass().getInterfaces()[1]);
}
}
op:-
this method is from class Test:
interface A
interface B
this method is from class Test:
interface A
interface B