Run base class's version of method in polymorphism

W

warren

Hi,

How do I call the base class's version of method when there is polymorphism?

also, what if there are multiple levels of inheritance and i want to call
the method that is
several levels up?

Thanks.
 
C

Cybertof

base.YourMethod();

For further levels, maybe this syntax should work :

base.base.base....base.YourMethod();


Regards,
Christophe.
 
W

warren

sorry. give a simple example below:

class A{
public virtual void Method(){
}
}

class B : A {
public override void Method(){
}
}

class C : B {
public override void Method(){
}
}

class Test{
static void Main(string[] args ){

C c = new C();

A a = c;
B b = c;

// if i use a.Method() or b.Method(), they will call class C's
version coz of polymorphism.
// so how do i call Method() of class A and B?
}
}
 
J

Jay B. Harlow [MVP - Outlook]

Warren,
As Cybertof stated, you can use base to call your immediate parents method.

To call grandparents or higher you will need to use reflection, I do not
have a sample of using reflection handy.

Hope this helps
Jay
 
C

Cybertof

Hum.....maybe i'm wrong but :

If B derives from A, and C derives from B, then it means that
A can be a particular B object
or
A can be a particular C object

if A is a B object, then A.Method() will call B.Method()
if A is a C object, then A.Method() will call C.Method()

In your example, you make A beeing a C object and B beeing a C object
too.
So,
A.Method() will call C.Method()
B.Method() will call C.Method()

Solution Hint :
Maybe if you cast A to an A object during the call ?
Or maybe if you box the A object into a real A type ?

A RealA = A;
RealA.Method() ?

Tell me if it works or if they are compilation errors.....

(I'm interested in knowing the answer too...)


Regards,
Christophe.
 
W

warren

Cybertof said:
base.YourMethod();

I access it in another class which has the Main, not within the subclass
itself.

For further levels, maybe this syntax should work :

base.base.base....base.YourMethod();


no. i know this doesn't work.
 
W

warren

Jay B. Harlow said:
Warren,
As Cybertof stated, you can use base to call your immediate parents
method.

if u see my simple example, i can't use the keyword base in the class Test.

To call grandparents or higher you will need to use reflection, I do not
have a sample of using reflection handy.

if so, then it is much more complex than what i thought. :(
 
J

Jon Skeet [C# MVP]

warren said:
I access it in another class which has the Main, not within the subclass
itself.

In that case, you can't do it - and very deliberately so, as it would
break encapsulation.
 
W

warren

Cybertof said:
Hum.....maybe i'm wrong but :

If B derives from A, and C derives from B, then it means that
A can be a particular B object
or
A can be a particular C object

if A is a B object, then A.Method() will call B.Method()
if A is a C object, then A.Method() will call C.Method()

In your example, you make A beeing a C object and B beeing a C object
too.
So,
A.Method() will call C.Method()
B.Method() will call C.Method()


Ya. That is the point of polymorphism. ;)

Solution Hint :
Maybe if you cast A to an A object during the call ?
Or maybe if you box the A object into a real A type ?

A RealA = A;
RealA.Method() ?

Tell me if it works or if they are compilation errors.....

(I'm interested in knowing the answer too...)

doesn't work after i tried. u don't need to do explicit cast,
because c 'is' an A. So explicit cast does the same as an implicit cast.
 
J

Jon Skeet [C# MVP]

Solution Hint :
Maybe if you cast A to an A object during the call ?

Nope - precisely because the call is polymorphic.
Or maybe if you box the A object into a real A type ?

A RealA = A;
RealA.Method() ?

That's not boxing, and it wouldn't do the trick either.

This is quite important, IMO, as otherwise callers could bypass very
deliberate restrictions etc of derived classes.
 
J

Jon Skeet [C# MVP]

Jay B. Harlow said:
To call grandparents or higher you will need to use reflection, I do not
have a sample of using reflection handy.

Apart from a couple of strange exceptional cases (GetHashCode being an
example, I think) this won't work either - the reflection call is
virtual too.
 
J

Jay B. Harlow [MVP - Outlook]

Jon,
I admit GetHashCode was the example I was thinking of.

I'll have to find my example, as I was thinking it would work for any
method.

I know VB.NET does something as its MyClass allows you to call the method
specific to that class, although there are derived classes, not quite the
same thing...

Although I agree with you, except for some very special exceptional cases
you should not be attempting to call a polymorphic function, in a specific
level of the hierarchy.

Thanks
Jay
 
1

100

Hi warren,
If you need to do that you don't need polymorphism. Just declare the methods
as non-virtual and you can call whatever method you want just by casting the
reference to type which method you want to call.
Anyway if you do need this you have to make it youirown. This is my
solution:

//----------------- Class A --------------------
class A
{
private string mName = "Class A";

public A RealA
{
get{return new A(this);}
}

protected A(A source)
{
//initlizes from the source
mName = source.mName;
}

public A()
{
}

public virtual void Foo()
{
Console.WriteLine("A");
}
}
//------------------------------------------
class B: A
{
private string mName = "Class B";

public B RealB
{
get{return new B(this);}
}

protected B(B source):base(source)
{
//initlizes from the source
mName = source.mName;

}

public B()
{
}

public override void Foo()
{
Console.WriteLine("B");
}

}

So now you can have

B b = new B();
A a = b;

b.Foo(); //Calls B.Foo()
b.RealA.Foo(); //Calls A.Foo()

a.Foo(); //Calls B.Foo
a.RealA.Foo(); //Calls A.Foo();

However, if we had had class C built following the same model we would have
called its method like:

C c = new C();
A a = c;

c.Foo(); //Calls C.Foo()
c.RealB.Foo(); //Calls B.Foo()
c.RealA.Foo(); //Calls A.Foo();


a.Foo(); //Calls C.Foo()
a.RealA.Foo(); //Calls A.Foo()
((B)a ).RealB.Foo(); //Calls B.Foo()
((C)a ).RealB.Foo(); //Calls B.Foo()

Got the picture?

HTH
B\rgds
100
 

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