CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Parent access child's methods

    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:
    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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Parent access child's methods

    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?

  3. #3
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Parent access child's methods

    BigEd,
    Thank you for the recommendation. polymorphism is just what I needed.

    Quote Originally Posted by BigEd781 View Post
    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

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Parent access child's methods

    Quote Originally Posted by kabilius View Post
    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:

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

  5. #5
    Join Date
    Jul 2006
    Posts
    297

    Re: Parent access child's methods

    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

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

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Parent access child's methods

    Quote Originally Posted by monalin View Post
    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

    Code:
    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.

  7. #7
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Parent access child's methods

    Quote Originally Posted by monalin View Post
    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.

    Code:
    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.

    Quote Originally Posted by BigEd781 View Post
    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.. :|

  8. #8
    Join Date
    Jul 2006
    Posts
    297

    Re: Parent access child's methods

    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.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Parent access child's methods

    Quote Originally Posted by kabilius View Post
    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.

  10. #10
    Join Date
    Jun 2007
    Location
    California
    Posts
    136

    Re: Parent access child's methods

    Quote Originally Posted by BigEd781 View Post
    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.

    Code:
    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

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Parent access child's methods

    Quote Originally Posted by kabilius View Post
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured