Is it any possible way to access the anonySubClassMethod method?
If no why Java compiler allowing to create this method?
abstract interface AnonyIfc {
public abstract void methodIfc ();
}
public class AnonyImplementation {
public static void main (String... a) {
AnonyIfc obj = new AnonyIfc(){
public void methodIfc() {
System.out.println("methodIfc");
}
public void anonySubClassMethod() {
System.out.println("anonySubClassMethod");
}
};
//obj.anonySubClassMethod() won't be visible since refering sub class
// method with super class reference
}
}
Update
From Francis Upton I understood that anonySubClassMethod can be used within the anonymous class. So can i expect the java compiler to restrict the access specifier to private for anonySubClassMethod? Hope there will be a reason for this public specifier also. just curious.
The method can be called from within your anonymous class. And the more typical use of this construct is cases where you are implementing an interface (for a GUI listener for example), so the method will be known since it’s an implementation of the interface the caller is expecting.