I have 3 classes. In 1 class I create an instance of a subclass inheriting from a superclass. How do I make my variables accessible to my subclass without it being available to the instance of the subclass? (Like static but only over multiple classes. I am new to java so at least that is how I think.)
public class Super {
protected int myInt; //Struggling here
}
public class Sub {
// Like to use the myInt in the super in a method here
}
public class MainPage {
Sub obj = new Sub();
int x = obj.myInt; //This should not happen
}
Subwill always have access tomyIntinternally (if it extendsSuperwhich seems to be missing in your example). What you want isMainPage, or any other class which is not a subclass ofSuper, to not have access tomyInt. In your example, when you doobj.myInt, it isMainPagethat tries to accessmyInt, notobj