I am writing a program where an overridded method must call a method in the parent class, which in turn must call the original method of the overridden method. The overridden method must then also call its parent method.
This is the code I have:
public class Cow
{
public float hiCat()
{
...
return meow;
}
public float dog()
{
float meowz = hiCat();
...
}
}
public class Moo extends Cow
{
public float hiCat()
{
float dogBark = dog();
float meows = super.hiCat();
...
}
}
Which gives me a stack overflow error like so:
Exception in thread "main" java.lang.StackOverflowError
at Moo.hiCat(Moo.java:12)
at Cow.dog(Cow.java:63)
Line 12 in Moo
is float dogBark = dog();
and line 63 in Cow
is float meowz = hiCat();
.
I believe line 63 is the problem in dog()
.
It seems like hiCat()
in class Moo
is calling dog()
in class Cow
like it's supposed to, but then dog()
is calling the overridded version of hiCat()
instead of the one it shares a class with. This confuses me because I am calling hiCat()
in a regular way with no modifiers or anything.
Why is this happening, and how can I fix it?
Note: the program's structure must remain the same.
Main method is completely unrelated to the methods at hand:
public static void main(String[] args)
{
InputClass input = new InputClass();
input.GenerateGUI();
}