-1

Using Netbeans 9 under Windows 10, I created in JAVA a class MyClass extends Anotherclass implements List, Serialize and accepted to override the methods from List, e.g. the following add method:

 @Override
    public boolean add(Object e) {
    boolean result = false;
    if (e instanceof Wohnung){ 
          add( (Wohnung) e);result=true;
    }
   return result;
   }

I call it from another class and when I try to use it I get a stack overflow error. Debugging it shows that the methos is called recursively. The code seems to be alright or not? Can anybody help me?

Thanks

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • There is an endless loop. You define method add and then call it from itself. – Al Kepp Dec 02 '18 at 11:44
  • You probably want `super.add()`. But extending ArrayList is a design smell already. – JB Nizet Dec 02 '18 at 11:47
  • @JBNizet The question is if he inherits the class or only implements the interface himself. If he only implements the intefrace, he has no base class to call. – Al Kepp Dec 02 '18 at 11:50

4 Answers4

1

You are getting this error due to recursive call to add() as you found out. The function works fine if e is not of type wohnung because you simply return result but when e is of type wohnung you are calling add() recursively and because there is no base case you get stack overflow error due to too many calls to add(). you need to call super method of add to solve this error.

Dhruv garg
  • 689
  • 7
  • 22
0

There is an endless loop. You define method add and then call it from itself. Your endless loop produces the stack overflow error.

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • Hello, thanks for your comments: The method add may recurse if not overriden in subclasses – Thommy 7571 Dec 02 '18 at 12:57
  • Hello, I had a problem editing the first comment..thanks for your comments: However I cannot call super since the parent class does not have this method. The herited method stems from the implemented arraylist not from the parent class.The class is linked to other classes as follows: public class MFHaus extends Haus implements List, IReadANDWriteAble, Serializable I get the message The method add may recurse if not overriden in subclasses. MF house means more "multi family house" Should I change the heritage structure? Is there another solution? ? House implements List? – Thommy 7571 Dec 02 '18 at 13:13
  • It does not make much sense to me to make a house implemnt a List but perhaps the reality cannot be described in that case by JAVA perfectly since more than one heritage is not possible... I understand better now why I get the same problem also using "return this.add(e)" in the code above, but as I said super is not possible in the present case... – Thommy 7571 Dec 02 '18 at 13:21
  • by the way the class Haus already inherits Address so cannot be extended either,,, – Thommy 7571 Dec 02 '18 at 14:36
  • Ich changed the structure as follows: "public class Haus extends Adresse implements List " and "public class MFHaus extends Haus implements IReadANDWriteAble, Serializable " Now I can use @Override public boolean add(Object e) { return super.add(e); } overring the method in Haus } } Now there is recursion at two places: 1. in the Class Haus since there is written inside the add method : this.add(obj); and secondly in the subclass MFHaus where I wrote inside the class super.add(obj); the upper class is still abstract – Thommy 7571 Dec 02 '18 at 16:16
0

Are you looking for something like this:

public class TokenSequence extends ArrayList<Object> {
  @Override
  public boolean add(Object e) {
    return super.add(e);
  }
}

Answer found here

As an edit:

As others have already described you are getting this error since you are calling the same method over and over again if you send in an object of type Wohnung. Imagine this; if object of type dog call add() but once inside of the add method you have written code that calls the same aadd() again thus the error.

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • thanks for the suggestion the code given by @Al Kepp does not work. there is no method .add(object) visible by "super...'" only methods from house . The second idea would work if there was no heritage of the house. It is not possible to herit in JAVA first from house than from ArraylList. Is it perhaps a visibilty problem of the methods in List? – Thommy 7571 Dec 02 '18 at 13:59
  • Get the feeling that you have a thought error in process – ThunD3eR Dec 03 '18 at 05:41
0

The problem is as @Al Kepp stated. Here's what you could use as a solution:

 @Override
    public boolean add(Object e) {
    boolean result = false;
    if (e instanceof Wohnung){ 
          super.add( (Wohnung) e);result=true;
    }
   return result;
   }
Raven
  • 2,951
  • 2
  • 26
  • 42