Accessing a parent class' methods
-
Say I have three classes, each a child of the previous. Can I explicitly call each of a classes methods that have been overridden?
class A
{
public A() { }
protected virtual void MakeLunch()
{
//Make cheese sandwich
}
}
.
class B : A
{
public B() { }
protected override void MakeLunch()
{
base.MakeLunch();
//Grab a banana
}
}
.
class C : B
{
public C() { }
protected override void MakeLunch()
{
A.MakeLunch();
//Get a drink
}
}From class C, can I skip the banana and just get a cheese sandwich and a drink? Or do I need one of my five-a-day?
My current favourite word is: Nipple!
-SK Genius
-
Say I have three classes, each a child of the previous. Can I explicitly call each of a classes methods that have been overridden?
class A
{
public A() { }
protected virtual void MakeLunch()
{
//Make cheese sandwich
}
}
.
class B : A
{
public B() { }
protected override void MakeLunch()
{
base.MakeLunch();
//Grab a banana
}
}
.
class C : B
{
public C() { }
protected override void MakeLunch()
{
A.MakeLunch();
//Get a drink
}
}From class C, can I skip the banana and just get a cheese sandwich and a drink? Or do I need one of my five-a-day?
My current favourite word is: Nipple!
-SK Genius
I wouldn't imagine you could since B overrides A C should not "know" about A. However, if B used new on MakeLunch instead then it would be possible by casting C as A.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
I wouldn't imagine you could since B overrides A C should not "know" about A. However, if B used new on MakeLunch instead then it would be possible by casting C as A.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
I figured which is why I initially hesitated in responding. However, I think the reasoning is why such an action is prevented by the compiler.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
I wouldn't imagine you could since B overrides A C should not "know" about A. However, if B used new on MakeLunch instead then it would be possible by casting C as A.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.Ennis Ray Lynch, Jr. wrote:
However, if B used new on MakeLunch instead then it would be possible by casting C as A.
But that won't work too since the
MakeLunch
is marked asprotected
. Please correct me if I am wrong.Navaneeth How to use google | Ask smart questions
-
Say I have three classes, each a child of the previous. Can I explicitly call each of a classes methods that have been overridden?
class A
{
public A() { }
protected virtual void MakeLunch()
{
//Make cheese sandwich
}
}
.
class B : A
{
public B() { }
protected override void MakeLunch()
{
base.MakeLunch();
//Grab a banana
}
}
.
class C : B
{
public C() { }
protected override void MakeLunch()
{
A.MakeLunch();
//Get a drink
}
}From class C, can I skip the banana and just get a cheese sandwich and a drink? Or do I need one of my five-a-day?
My current favourite word is: Nipple!
-SK Genius
It does not work in C# , but it is possible at IL level. So I guess its simply a rule that prevents you to skip layers of abstraction in C#.
-
Ennis Ray Lynch, Jr. wrote:
However, if B used new on MakeLunch instead then it would be possible by casting C as A.
But that won't work too since the
MakeLunch
is marked asprotected
. Please correct me if I am wrong.Navaneeth How to use google | Ask smart questions
I haven't tried it but it might, since C would still be in the inheritance chain for A
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.