473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Run base class's version of method in polymorphism

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.
Nov 15 '05 #1
12 1780
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?
}
}
Nov 15 '05 #2
base.YourMethod();

For further levels, maybe this syntax should work :

base.base.base....base.YourMethod();
Regards,
Christophe.

In article <3f********@news.starhub.net.sg>, wa*********@yahoo.com
says...
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.

Nov 15 '05 #3

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.
Nov 15 '05 #4
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

"warren" <wa*********@yahoo.com> wrote in message
news:3f********@news.starhub.net.sg...
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.

Nov 15 '05 #5
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.

In article <3f********@news.starhub.net.sg>, wa*********@yahoo.com
says...
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?
}
}

Nov 15 '05 #6

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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. :(
Nov 15 '05 #7

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

Nov 15 '05 #8
warren <wa*********@yahoo.com> wrote:
base.YourMethod();


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Cybertof <cy****************@gmx.net> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12
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

Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
6
by: Daniel Billingsley | last post by:
I understand the conditions where you have to use base., but does it make any sense to use it elsewhere to make clear to the reader that the method or property you refer to comes from the base...
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, (sorry to begin with Java in a Python list ;-) in Java, when I want to pass input to a function, I pass "InputStream", which is a base class of any input stream. In Python, I found...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.