Friday, 16 August 2013

Why does this provide me with a stack overflow error?

Why does this provide me with a stack overflow error?

The following code yields a stack overflow error, can someone tell me why?
(I know how to fix the error, if I reference 'super' instead of 'this' in
the add method, but I am not sure why that works.)
package subclassingVector;
import java.util.Vector;
public class MyVectorSubclass extends Vector<MyModelClass> {
public MyVectorSubclass(){
MyModelClass m = new MyModelClass();
m.setId(2);
this.add(m);
if(this.contains(m)){
System.out.println("contains is true");
}
}
public boolean add(MyModelClass o){
this.add(o);
return true;
}
public boolean contains(Object o){
for(subclassingVector.MyModelClass mmc: this){
if(mmc.equals((MyModelClass) o)){
return true;
}
}
return false;
}
public static void main(String[] args) {
String s = new String("SSEE");
MyVectorSubclass m = new MyVectorSubclass();
}
public class MyModelClass {
private Integer id = null;
public MyModelClass() {
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}

No comments:

Post a Comment