-
Dec 26th, 2007, 06:45 PM
#1
Thread Starter
Frenzied Member
[2.0] Access Modifier
Is there an access modifier that makes a method public in the superclass and not existent in its subclass? Sort of like a mix of private and protected. I tried hiding the method in the subclass using the new keyword, but since there are overloads of the same method in the superclass, it only hides one of the overloads, which is not what I need.
Thanks
-
Dec 26th, 2007, 06:53 PM
#2
Thread Starter
Frenzied Member
Re: [2.0] Access Modifier
Actually, I have just modified the design of my code so this isn't really needed, but it would still be nice to know.
-
Dec 26th, 2007, 09:32 PM
#3
Re: [2.0] Access Modifier
There's no way to do that. All you can do is override/shadow the base implementation and either throw an exception or just leave the body empty.
It should be noted, though, that overriding follows the object while shadowing follows the type. That means that if you override a member in a derived class then cast an instance as the base type, it will still be the derived implementation of the member that gets invoked. If you shadow a member, on the other hand, casting as the base type gives access to the base implementation of the member shadowed in the derived type. Try this to see what I mean:
CSharp Code:
public class BaseClass
{
public virtual void Method1()
{
MessageBox.Show("BaseClass", "Method1");
}
public void Method2()
{
MessageBox.Show("BaseClass", "Method2");
}
}
public class DerivedClass : BaseClass
{
public override void Method1()
{
MessageBox.Show("DerivedClass", "Method1");
}
public new void Method2()
{
MessageBox.Show("DerivedClass", "Method2");
}
}
CSharp Code:
BaseClass b1 = new BaseClass();
b1.Method1();
b1.Method2();
DerivedClass d = new DerivedClass();
d.Method1();
d.Method2();
BaseClass b2 = (BaseClass)d;
b2.Method1();
b2.Method2();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|