neodatatype Posted September 16, 2003 Posted September 16, 2003 Hi all, I've a problem with inheritance. Let's see 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! > NeoDataType.net < Try my Free .Net Reporting Tool!
Administrators PlausiblyDamp Posted September 16, 2003 Administrators Posted September 16, 2003 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 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??? } } Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
neodatatype Posted September 17, 2003 Author Posted September 17, 2003 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 :) > NeoDataType.net < Try my Free .Net Reporting Tool!
Recommended Posts