G
gk
code-1
-------------
package packageX;
public class SuperclassX {
protected void superclassMethodX() {
}
protected int superclassVarX;
}
code-2:
------------
package packageY;
import packageX.*;
public class SubclassY extends SuperclassX
{
SuperclassX objX = new SubclassY();
SubclassY objY = new SubclassY();
void subclassMethodY()
{
objY.superclassMethodX();
int i;
i = objX.superclassVarX; // compile error is here
}
}
Not understanding why there is compile error ?
objX is a type (reference to the Subclass object ) and compiler is
always happy to check with the type .....the objX type really indeed
have superclassVarX as a protected member ....and so there should
not have been any compile error.
I am getting a compile error which is not understandable.
please explain .
thank you
-------------
package packageX;
public class SuperclassX {
protected void superclassMethodX() {
}
protected int superclassVarX;
}
code-2:
------------
package packageY;
import packageX.*;
public class SubclassY extends SuperclassX
{
SuperclassX objX = new SubclassY();
SubclassY objY = new SubclassY();
void subclassMethodY()
{
objY.superclassMethodX();
int i;
i = objX.superclassVarX; // compile error is here
}
}
Not understanding why there is compile error ?
objX is a type (reference to the Subclass object ) and compiler is
always happy to check with the type .....the objX type really indeed
have superclassVarX as a protected member ....and so there should
not have been any compile error.
I am getting a compile error which is not understandable.
please explain .
thank you