You can call the method on an object that implements that method, so long as it is accessible from your current class. In particular, since the getRace
method is public, any class can call it so long as they have an appropriate instance of Orc
or Human
to call it on.
I expect that your sticking point is that you have a list of Karakter
s, rather than Orcs or Humans. Thus Karakters
only knows at that point that the object is a Karakter
, and so can only call methods defined in the Karakter
class/interface.
One solution is to add the getRace
method to the Karakter
class as well. If it doesn't make sense for this to return a value in the superclass, then you can make the superclass abstract (which means you cannot construct an instance of it directly, you can only construct one of its subclasses), and declare the method abstract in Karakter
too:
public abstract class Karakter
{
/*
...
... Same as before
...
*/
public abstract String getRace();
}
This forces the subclasses to have an implementation of getRace
(which isn't a problem here, as they do anyway) and means that Karakters
can now be sure that no matter what kind of Karakter
object it has, there is a getRace() method to call.
This is just one approach to the solution based on what I understand your intent to be. But the underlying issue is that the Karakter
class doesn't define getRace
, and so the method cannot be called directly on references of that type.