Inheritance and modifiers
-
Wednesday, February 22, 2012 7:36 PM
Hello everyone, i'm having problems with inheritance and access modifiers.
Well, here's the interface code IParent:
internal interface IParent { void doStuff(); }I have a class that implements the intercafe IParent, called Parent:
internal class Parent : IParent { public void doStuff() { // implementation } }The interface and class are restricted only the same assembly due to the internal modifier and this is my goal. But i have a class Child that override the method doStuff(). This class i want to be visible for other assemblies:
public class Child : Parent { public new void doStuff() { // more implementations base.doStuff(); } }But the compiler does not accept. The erros says the class Child is less accessible then the class Parent. Well is that right? How to solve my problem? I can modifie the modifier of class Parent to public and call the method direct like:
new Parent().doStuff();
But it may give error! I want:
new Child().doStuff();
I took a test in Java and works! Only instead of internal modifier i use the default (no modifier) thats restricts access to package. Thaks!
Answers
-
Wednesday, February 22, 2012 7:48 PMModerator
You can't do this in C# -
Inheritance is a public contract - allowing this would basically be saying "The Child class is a specific form of [something I'm hiding from you]", which is not allowed (and somewhat confusing).
Typically, this situation can be handled via encapsulation instead of inheritance. You can do something like:
public class Child { public Child() { this.Parent = new Parent(); } internal Parent Parent { get; private set; } public void DoStuff() { // Do your stuff... this.Parent.DoStuff(); } }
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by andreylh Wednesday, February 22, 2012 7:51 PM
-
Wednesday, February 22, 2012 7:51 PM
You won't be able to do this. You cannot have a public child if it's parent is hidden (to something outside of the assembly.- Marked As Answer by andreylh Thursday, February 23, 2012 11:21 AM
-
Thursday, February 23, 2012 10:39 AM
If you want to prevent the base class from being instantiated, you can make its constructor internal.- Marked As Answer by andreylh Thursday, February 23, 2012 11:21 AM
All Replies
-
Wednesday, February 22, 2012 7:48 PMModerator
You can't do this in C# -
Inheritance is a public contract - allowing this would basically be saying "The Child class is a specific form of [something I'm hiding from you]", which is not allowed (and somewhat confusing).
Typically, this situation can be handled via encapsulation instead of inheritance. You can do something like:
public class Child { public Child() { this.Parent = new Parent(); } internal Parent Parent { get; private set; } public void DoStuff() { // Do your stuff... this.Parent.DoStuff(); } }
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by andreylh Wednesday, February 22, 2012 7:51 PM
-
Wednesday, February 22, 2012 7:51 PM
You won't be able to do this. You cannot have a public child if it's parent is hidden (to something outside of the assembly.- Marked As Answer by andreylh Thursday, February 23, 2012 11:21 AM
-
Thursday, February 23, 2012 10:39 AM
If you want to prevent the base class from being instantiated, you can make its constructor internal.- Marked As Answer by andreylh Thursday, February 23, 2012 11:21 AM

