I have tree classes.
class MyObject{
public void DoSomething()
{
here I need to call method add from class base.
}
}
class base
{
protected final void add(){}
}
class extended extends base {
private MyObject pObject = new MyObject();
...
{
pObject.DoSomething();
}
}
I could have created class for each variation that extends class extended, but the type what I need to use becomes available only after class extended is already initiated.
How do I call base.add() from MyObject inner method?
You can do it in a couple of ways:
extendedclass inMyObjectclass. When you instantiateMyObjectvariable inextendedclass, pass it the reference ofextended.Something like this:
baseclassstatic. This way you can call the methods without requiring an instance of thebaseclass.Something like this:
One more thing: This is off-topic, but you might want to read about Java Naming Conventions. Having class names start with lowercase is something that you wouldn’t find in the naming conventions.