Friday, March 21, 2008

Interfaces and Classes and unambiguous references

What if a class implements two interfaces, and both the interfaces have static or instance variables with the same name?

Example:

public interface InterfaceA {

static String name="Manish from A";
public String getName();

}

public interface InterfaceB {

static String name="Manish from A";
public String getName();

}

public class Main implements InterfaceA,InterfaceB {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main();
System.out.println(main.getName());
}

public String getName() {
return name;
//throw new UnsupportedOperationException("Not supported yet.");
}

This kind of implementation will throw an unambiguous reference too the object name error during compile time.