How to call the grandparent's method

A

ad

base only can call the parent 's method
The result of the example below is
I am Parent
I am Child

How can we call the grandparent's method in a inhreited class,

I want the result is

I am GrandParent
I am Child

--------------------------------------------------------------------------
using System;

class Test
{
static void Main()
{
Child aChild = new Child() ;
aChild.WhoAmI();
Console.Read() ;
}
}

class GrandParent
{

public virtual void WhoAmI()
{
Console.WriteLine("I am GrandParent");
}

}

class Parent : GrandParent

{

public virtual void WhoAmI()
{
Console.WriteLine("I am Parent");
}

}

class Child : Parent

{

public virtual void WhoAmI()
{
base.WhoAmI();
Console.WriteLine("I am Child");
}

}
 
R

Ranjan

use (this as GrandParent) in the Child's call. i.e., change the Child's
WhoAmI function to

public override void WhoAmI()
{
(this as GrandParent).WhoAmI();
Console.WriteLine("I am Child");
}



What you are doing is not a good programming practice (as the compiler will
tell you with its warnings).
1.cs(27,21): warning CS0114: 'Parent.WhoAmI()' hides inherited member
'GrandParent.WhoAmI()'. To make the current member override that
implementation, add the override keyword. Otherwise add the new
keyword.
1.cs(16,21): (Location of symbol related to previous warning)
1.cs(38,21): warning CS0114: 'Child.WhoAmI()' hides inherited member
'Parent.WhoAmI()'. To make the current member override that
implementation, add the override keyword. Otherwise add the new
keyword.
1.cs(27,21): (Location of symbol related to previous warning)


You should override inherited virtual functions if you are implementing new
functionality.
Ranjan
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You cannot do it directly, you only have access to your parent.
It's considered a BAD practice doing what you want, you should use your
parent version , if it was redefined there it was done for a reason
afterall. Remember that it may be possible that the method does not even
exist in your grandparent definition.

IF for some reason you need to access it, there are two ways of doing it.
1- If you know your grandparen type you could do a cast:
((GrandParent)this).Method()

2- If you do not know it you may use reflection to know your grandparent
type and do a similar construction as in the above example


Anyway, remember it's a bad practice doing that.

Cheers,
 
R

Rachel Suddeth

First of all, you want the parent and the child definitions to start like
this....
public override void WhoAmI()
Then, the first line in each should be
base.WhoAmI();
You remembered to put that in the child, but it needed to be in the parent,
too, if you wanted the grandparent base method to be called.

-Rachel
 
B

Bruce Wood

Note that Ignacio's example of how to do this will work only if you use
the "new" keyword to declare successive WhoAmI() methods in the Parent
and Child. If you use the "override" keyword in the Parent.WhoAmI() and
Child.WhoAmI() method declarations (which is the usual practice) then
his example will print out "I am Child" because even though the object
is cast to Grandparent, polymorphism will take over and invoke the
child class's override for the method.

Everyone is pointing out that this is bad practice because you never
explained what problem you are trying to solve with this technique.
What are you trying to do? Perhaps if we better understood your
original problem we could propose a better solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top