Closed proninyaroslav closed 5 years ago
did you call super.init()
in your override? and you should not need to call it in your constructor. In any case, I have seconds thoughts about this design, since it's an anti-pattern to call a virtual method in a constructor, more so if it's the "actual constructor" such a method.
I thought that calling super.init()
in an overridden method is superfluous, as it is already done in the superclass constructor.
actually, it's not :)
This is an example with problem:
public AdvancedPeerInfo(peer_info p) {
super(p);
init(p);
}
@Override
protected void init(peer_info p) {
//init fields
}
An example without problem:
public AdvancedPeerInfo(peer_info p) {
super(p);
//init fields
}
Make it like this
public AdvancedPeeeInfo(peer_info p) {
super(p);
}
@Override
protected void init(peer_info p) {
super.init(p);
//your extra logic here
}
Thanks, every time I open new things in Java)
Hi. I needed to extend
PeerInfo
class and add some fields (port
,pieces
). I inherited this class, and also overrideinit()
method to fill in my own fields. I saw a warning comment. As a result, if I overrideinit()
method and call it in constructor, after this all fields of the superclass become null or zero (ip
,client
, etc). So I deleted the overridden method and fill fields in constructor, and it works great. Why is this happening?