Overriding of method.

A

archana

Hi all,

I have one base class and 2 derived class and one method say xyz.

in base class i declare xyz as virtual, but i want to override to
override this method in derived2 class and not in derived1 class. but
in derived1 class i want method with same name that is 'xyz'. but don't
want to override here. means say i have following code

class base1
{

public virtual void xyz()
{
System.Console.WriteLine("base1 xyz");
}
}

class derived1 : base1
{

public void xyz()
{
System.Console.WriteLine("derived1 xyz");
}

}

class derived2 : derived1
{
public override void xyz()
{
System.Console.WriteLine("derived2 xyz");
}
}

Above code is giving me error. Can anyone tell me why this is
happening. Can't i override base class method in derived2 .

Plese correct me if i am wrong.

thanks in advance.
 
C

Christof Nordiek

Hi archana,

the problem is, you try to override a hidden member. That isn't possible in
C#.
Since base1.xyz is hidden in derived1 and derived2 derives from derived1,
the name xyz in derived2 referes to derived1.xyz. The compiler tries to
override derived1.xyz, wich is not possible because it isn't virtual.
You could make derived1 virtual, but that would have another semantic.

Why you want to hide the method at all. You should consider, giving it
another name.

hth
Christof
 
S

Super Robert

That's not difficult
You just define the method like this

public new void xyz()
{
System.Console.WriteLine("derived1 xyz");
}
 
M

Marc Gravell

Others have mentioned the "new" modifier. For completeness, when I see
this usage my default reaction (in the absense of code-comments giving
a good reason) is "this should be using an interface". Interface-based
programming allows this type of re-declaration in a much cleaner way,
and allows you to (successfully) have multiple *identical* methods
with the same name and signature (using explicit implementations)
without breaking your ability to override etc.

For instance, ICloneable might be implemented to be:


public SomeClass Clone() {
// return a new copy
}
object ICloneable.Clone() {
return Clone(); // refers to this.Clone, the SomeClass version
}

Marc
 
J

Jon Skeet [C# MVP]

Super said:
That's not difficult
You just define the method like this

public new void xyz()
{
System.Console.WriteLine("derived1 xyz");
}

That doesn't solve the problem at all. It removes a warning from
derived1, but it doesn't allow the OP to override the base class's
method in derived2, which has been hidden by derived1. I don't believe
you *can* override the base class's method in a doubly-derived class
where the original method has been hidden by the parent.

Just another reason for not hiding methods...

Jon
 
S

Super Robert

"Jon Skeet [C# MVP] дµÀ£º
"
That doesn't solve the problem at all. It removes a warning from
derived1, but it doesn't allow the OP to override the base class's
method in derived2, which has been hidden by derived1. I don't believe
you *can* override the base class's method in a doubly-derived class
where the original method has been hidden by the parent.

Just another reason for not hiding methods...

Jon


I'm not quite clearly about your meaning. I think it's OK about this .
In derived1 class hide the base method
In derived2 class use the virtual property to override the base method
Here's the whole codes

namespace abc
{
class Base1
{
public virtual void xyz()
{
System.Console.WriteLine("base 1 xyz");

}
}

class Derived1:Base1
{
public new void xyz()
{
System.Console.WriteLine("Derived 1 xyz");
}
}

class Derived2:Base1
{
public override void xyz()
{
Console.WriteLine("Derived 2 xyz");
}
}

class Program
{
static void Main(string[] args)
{
Base1 a = new Base1();
Derived1 b = new Derived1();
Derived2 c = new Derived2();
a.xyz();
b.xyz();
c.xyz();
}
}
}
 
C

Christof Nordiek

Hi,

look carefully:

In the example of the OP Base2 derives from Derived1, not from Base1.



"Jon Skeet [C# MVP] ??:
"
That doesn't solve the problem at all. It removes a warning from
derived1, but it doesn't allow the OP to override the base class's
method in derived2, which has been hidden by derived1. I don't believe
you *can* override the base class's method in a doubly-derived class
where the original method has been hidden by the parent.

Just another reason for not hiding methods...

Jon


I'm not quite clearly about your meaning. I think it's OK about this .
In derived1 class hide the base method
In derived2 class use the virtual property to override the base method
Here's the whole codes

namespace abc
{
class Base1
{
public virtual void xyz()
{
System.Console.WriteLine("base 1 xyz");

}
}

class Derived1:Base1
{
public new void xyz()
{
System.Console.WriteLine("Derived 1 xyz");
}
}

class Derived2:Base1
{
public override void xyz()
{
Console.WriteLine("Derived 2 xyz");
}
}

class Program
{
static void Main(string[] args)
{
Base1 a = new Base1();
Derived1 b = new Derived1();
Derived2 c = new Derived2();
a.xyz();
b.xyz();
c.xyz();
}
}
}
 
D

Duggi

Hi Archana,

Methods that are hidden in the inheritence tree **can not** be
overridden, because the method is no longer visible, down the tree.

I think you should revisit the design of the classes, better go for
interface based programming.

Thanks
-Srinivas.
 

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