Click to See Complete Forum and Search --> : Parent access child's methods


kabilius
August 17th, 2009, 01:31 PM
Hi all,

I am trying to access a child's method from parent.. (it seems like the wrong thing to do, but I am kind of out of ideas)

Here is the scenario.
A parent, ParentOne, has 2 sub-classes - ChildOne and ChildTwo.
Both ParentOne, ChildOne and ChildTwo implements an interface method called - CalculateDistance().

Here is my code:

ParentOne distance;
distance = new ChildTwo();
distance.CalculateDistance();


When I execute the code, it is using the CalculateDistance() from the ParentOne, but I really want the ChildTwo's CalculateDistance() to be called.

and I can't seem to figure out the way to do it.
Any idea?

Thank you in advance for the help,
kab

BigEd781
August 17th, 2009, 01:41 PM
You need to make the CalculateDistance( ) method virtual and override it in your subclasses. This is called polymorphism and once you do that everything will work as expected.

On a side note, since they all implement the same interface, why not create an interface object instead of using the base class?

kabilius
August 17th, 2009, 04:52 PM
BigEd,
Thank you for the recommendation. polymorphism is just what I needed.

On a side note, since they all implement the same interface, why not create an interface object instead of using the base class?

The reason I cannot create an interface object is because the method CalculateDistance() has a class variable that's not local to the CalculateDistance(). If I create an interface object, I won't have access to that variable anymore.

Thanks a bunch,
kab

BigEd781
August 17th, 2009, 05:04 PM
BigEd,
The reason I cannot create an interface object is because the method CalculateDistance() has a class variable that's not local to the CalculateDistance(). If I create an interface object, I won't have access to that variable anymore.

Thanks a bunch,
kab

Sure you would. The implementation is separate from the interface. How each class implements CalculateDistance( ) is irrelevant. The point is that the caller does not have to know. So, you would write:


IParent distance = new ChildTwo();
distance.CalculateDistance();

monalin
August 17th, 2009, 05:18 PM
I think what he's trying to say is that the class ParentOne has a property in it that does not exist in the Interface and he needs access to that property and thats why he cannot cast it as an interface and must use


ParentOne distance;
distance = new ChildTwo();
distance.CalculateDistance();

BigEd781
August 17th, 2009, 05:21 PM
I think what he's trying to say is that the class ParentOne has a property in it that does not exist in the Interface and he needs access to that property and thats why he cannot cast it as an interface and must use


ParentOne distance;
distance = new ChildTwo();
distance.CalculateDistance();


I thought about that, but the OP said


because the method CalculateDistance() has a class variable that's not local to the CalculateDistance()


So I wasn't sure. It sounded like the variable was used inside of the CalculateDistance method.

kabilius
August 17th, 2009, 05:43 PM
I think what he's trying to say is that the class ParentOne has a property in it that does not exist in the Interface and he needs access to that property

Hmm, I guess I should have said, "class ChildTwo has a property in it that does not exist in the interface"
My code is something like this right now.


public class ChildTwo : ParentOne
{
private int nTotalCost; // another function will calculate the cost first
private int nCostPerDistance = 20;

public override int CalculateDistance()
{
return nTotalCost / nCostPerDistance;
}
}


nTotalCost is not in the interface, and it does not exist in neither ParentOne nor in ChildOne.

So I wasn't sure. It sounded like the variable was used inside of the CalculateDistance method.
Sorry, I don't know the correct term to describe nTotalCost.. :|

monalin
August 17th, 2009, 05:51 PM
Yeah see in that situation it doesn't matter. An interface would do the job here as well. Like BigEd said, the interface does not care whats on the inside it only cares about the function signature i.e. name, parameters, return type, and get/set accessors. Depending on the rest of your code though an interface may or may not be what you need.

BigEd781
August 17th, 2009, 05:52 PM
nTotalCost is not in the interface, and it does not exist in neither ParentOne nor in ChildOne.


Sorry, I don't know the correct term to describe nTotalCost.. :|

It doesn't matter if nTotalCost is in the interface. The interface simply defines a set of methods and properties that must be implemented. How those methods are implemented makes no difference, i.e., if one class uses instance variables for its calculation and one does not, it doesn't matter because each class may provide their own implementation.

EDIT: monalin beat me :). As he suggested, an interface isn't always the correct solution. Sometimes an abstract base class is better suited to the job, but I definitely think you would gain some flexibility here by using one of them.

kabilius
August 17th, 2009, 10:43 PM
It doesn't matter if nTotalCost is in the interface. The interface simply defines a set of methods and properties that must be implemented. How those methods are implemented makes no difference, i.e., if one class uses instance variables for its calculation and one does not, it doesn't matter because each class may provide their own implementation.

EDIT: monalin beat me :). As he suggested, an interface isn't always the correct solution. Sometimes an abstract base class is better suited to the job, but I definitely think you would gain some flexibility here by using one of them.

Hmm, I guess when you guys keep asking questions, you are finding out more about what I am doing. :)

Here is a concept on what I am doing.


ParentOne distance = new ChildTwo();
distance.

was instantiated in another part of the program, then nTotalCost was calculated. So now, the distance object has a valid value for nTotalCost. Then distance was passed to another class constructor as a parameter; however, the constructor only knows about ParentOne but not ChildTwo.
In the new class, I want to call the CalculateDistance() of the ChildTwo.

If I implement this with an interface object, I would lose the value of nTotalCost.. (I think)
I am using the polymorphism as you described and it's working perfectly. Do you think making an abstract class is more appropriate here?

Thank you both!!
kab

Arjay
August 17th, 2009, 11:37 PM
If I implement this with an interface object, I would lose the value of nTotalCost.. (I think)Why not code up a simple app and try it?