Hiding a base method

Status
Not open for further replies.

neodatatype

Regular
Joined
Aug 18, 2003
Messages
65
Location
Italy
Hi all,

I've a problem with inheritance.

Let's see
C#:
using System;

public class classBase {
     public void Method(string str) {
     }
}

public class classDerived : classBase {

     private new void Method(string str) {
     }

}

class cMain {

     static void Main() {
         classDerived cl = new classDerived();
         cl.Condizione(3);
     }
}

I hoped that with the "new" keywork I can hide the method Method, cause of the "private" keyword, but I still have access to it.

How can I hide methods of a base class?

Thank you!
 
Would just having a dummy version that does nothing not work.

Out of interest why are you trying to hide the base method? If it isn't required by all subclasses it shouldn't be in the base class. hiding it would effectively break inheritance. Imagine the following code

C#:
using System;

//your code follows
public class classBase {
     public void Method(string str) {
     }
}

public class classDerived : classBase {

     private new void Method(string str) {
     }

}

class cMain {

     static void Main() {
         classDerived cl = new classDerived();
         cl.Condizione(3);
     }
//extra code

static void Test(classBase B)
{
B.Method("test");    //what would happen here if a classDerived Object was passed in???
}
}
 
Out of interest why are you trying to hide the base method? If it isn't required by all subclasses it shouldn't be in the base class. hiding it would effectively break inheritance. Imagine the following code

Because I have these classes:

A that works on B type

B, a base class

C : B
D : B

C and D should be able to be treated by A (A is just a typized collection for objects of type B) but they have some methods that are differents from that in B, so when I call D, I want that D haven't a method defined in B.

I hope I'm clearler :)
 
Status
Not open for further replies.
Back
Top