lundi 22 décembre 2014

Java Self Reference with Inhertiance

I understand why this code below doesn't work. That's because convolusion will call Base, not Derived. This code is soooo simple, and have self-referencing. I extended 'self-referencing class' and I stucked with that problem.



class Base
{
public int important_data;
protected Base child;

public int sum()
{
if(child != null)
{
return important_data + child.sum();
}
else
{
return important_data;
}
}
}

class Derived extends Base
{
public int more_important;

public int convolusion()
{
if(child != null)
{
return more_important*important_data + child.convolusion();
}
else
{
return more_important*important_data;
}
}
}


Then, is there any available method to do that?


Aucun commentaire:

Enregistrer un commentaire