473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Overriding of method.

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.

Jan 9 '07 #1
8 1479
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

"archana" <tr**************@yahoo.comschrieb im Newsbeitrag
news:11**********************@42g2000cwt.googlegro ups.com...
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.

Jan 9 '07 #2
That's not difficult
You just define the method like this

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

Jan 9 '07 #3
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
Jan 9 '07 #4
Super Robert wrote:
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

Jan 9 '07 #5

"Jon Skeet [C# MVP] дµÀ£º
"
Super Robert wrote:
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

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();
}
}
}

Jan 10 '07 #6
Hi,

look carefully:

In the example of the OP Base2 derives from Derived1, not from Base1.
"Super Robert" <ro***********@gmail.comschrieb im Newsbeitrag
news:11**********************@k58g2000hse.googlegr oups.com...

"Jon Skeet [C# MVP] ??:
"
Super Robert wrote:
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

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();
}
}
}
Jan 10 '07 #7
I see. That's my fault. Of course Derived2 cannot inherit Derived1.
Thanks.

Jan 10 '07 #8
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.
archana wrote:
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.
Jan 10 '07 #9

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

Similar topics

3
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
6
by: John Cobb | last post by:
MSDN and intellisense shows the Add method of Hashtable being overridable however when I use this code: Public Class RAL Inherits Hashtable Public Overrides Sub Add(ByVal Key As String, ByVal...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
5
by: kj | last post by:
I'm trying to subclass file, overriding the readline method. The new method definition begins with def readline(self, size=None): line = self.file.readline(size) # etc., etc. ....where the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.