interface iMyInterface {
public iMethod1();
}
public class cMyClass implements iMyInterface{
public iMethod1() {
System.out.println("From Method1");
}
protected iMethod2() {
System.out.println("From Method2");
}
}
class AppMain
{
iMyInterface i=new cMyClass();
public static void main(){
i.iMethod1();
((cMyClass)i).iMethod2();
}
}
this produce output as follows
From Method1
From Method2
becoz interface object is casted to that class
but my question is i cannot cast in this following case
class AppMain
{
iMyInterface i=new cMyClass();
public static void main(){
i.iMethod1();
this.((cMyClass)i).iMethod2();
}
}
Eclipse IDE shows following error:
Syntax error on token “.”, Identifier expected after this token.
i don’t understand this
in either way i accessing the same field.
You’re just casting at the wrong point. You want:
Not that you have a
thisto refer to in a static method likemainin your example…(Also note that none of your types etc follow Java naming conventions…)