manuk
manuk

Reputation: 23

Infinite recursion error

I have a parent class which has the following method in it:

public String getSubjects() {
    return this.getSubjects();    
} 

And in the subclass which inherits this method is the following:

@Override
    public String getSubjects(){
        return(" Computer Student" + "[" + studCountObj + "] " + name + ": "); 
    }

But I get an error saying:

The method getSubjects may recurse if not overridden in subclass.

What does this mean and what can I do to fix this issue?

Upvotes: 1

Views: 922

Answers (2)

Pierre
Pierre

Reputation: 31

this.getSubjects() refer to the current method. If you want to call the method of the parent class, user keyword "super" :

public String getSubjects() {
    return super.getSubjects();    
} 

Upvotes: 0

Mureinik
Mureinik

Reputation: 311163

The parent's class' implementation just calls itself, and as the error notes, will continue to do so, endlessly. The parent class should either return some default (e.g., null), or, even better, leave this method abstract if there's no intelligent way it can implement the method.

Upvotes: 1

Related Questions