9
public class GrandParent
{
    public void walk()
    {
        ...
    }
}

public class Parent
{
    public void walk()
    {
        ...
    }
}

public class Child
{
    public void walk()
    {
        // Here in some cases I want to use walk method of GrandParent class
    }
}

Now in Child.walk(), I want to use GrandParent.walk() only in some cases. How can I do that? Since super.walk() will always use Parent.walk().

NOTE: (Please note that this is simplified example of a complex scenario)

NOTE2: Please only tell if there is a standard procedure of doing that. I have used a flag in parent class, which the child sets if it wants to use the method of GrandParent. But this sequence becomes very complex.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • Put the same logic you use in `Child` into `Parent`, too. –  Mar 11 '13 at 08:10
  • Can't you just cast the child instance to the grandparent type and call walk on that? – 0xCAFEBABE Mar 11 '13 at 08:12
  • 5
    Just as a side note, you may be interested in knowing that you are breaking encapsulation by trying to access GrandParent functionality from Child. Merely a "good design" point, but I felt that it was relevant. For more, see http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java – Rahul Banerjee Mar 11 '13 at 08:13
  • @0xCAFEBABE The most specific implementation of the method is called. By casting the child to grandparent won't change the availability of the most specific implementation. –  Mar 11 '13 at 08:15
  • The terminology confuses me a bit, am used to hearing `walk` and `parent` used together when describing object trees, not class inheritance. – flup Mar 11 '13 at 08:15
  • Seems like you have a design problem. You are not suppose to do that. It would be helpful to give us some background: Why you need to do this?? – codeMan Mar 11 '13 at 08:17
  • Is it possible to make `walk` function parametrise – Rais Alam Mar 11 '13 at 08:27
  • @Real yep it is possible but that's not much different from creating a flag in parent classes. –  Mar 11 '13 at 08:57

2 Answers2

8

The problem here is in the decision to call the Parent function "walk". It should not have been called "walk" unless it is intended to be a complete functional replacement for the GrandParent walk().

If that decision cannot be changed, then the solution suggested in the existing answer from @zvzdhk is the best that can be done.

Ideally, the Parent walk() would be given a new name reflecting whatever is different about its function from the GrandParent walk(). Each of the functions could then be called from the Child class.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
6

You could specify method for accessing parent's value like this:

public class GrandParent
{
    public void walk()
    {
        ...
    }
}

public class Parent
{
    public void walk()
    {
        ...
    }

    public void grandParentWalk()
    {
        super.walk();
    }
}

public class Child
{
    public void walk()
    {
        grandParentWalk();
    }
}

Similar questions:

Community
  • 1
  • 1
bsiamionau
  • 8,099
  • 4
  • 46
  • 73