What is the purpose of internal abstract method in a abstract class? why to make an abstract method internal in a abstract class? if we want to restrict abstract class outside the assembly why don't we just make abstract internal class. Is there any other logic behind it.
Adding internal
member to a public
abstract class makes it impossible to inherit that abstract
class outside of assembly it was declared in. But the class itself as well as all derived classes can still be used (as the types are public
) outside of declaring assembly.
Let's say you have an abstract class:
public abstract AMyClass
{
public string DoSomething()
{
return DoSomethingInternal();
}
internal abstract string DoSomethingInternal();
}
And another public class that inherits it, declared in the same assembly
public sealed MyClass : AMyClass
{
internal override string DoSomethingInternal()
{
return "Hey from MyClass!";
}
}
You could still create an instance of MyClass
in different assemblies, but you won't be able to implement your own class that derives from AMyClass
, because you won't be able to implement abstract DoSomethingInternal
method.
Imagine you have a code in your project
lets say its a console app and we call it as ConsoleApplication1
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{}
}
public abstract class MyAbsClass
{
public string DoSomething()
{
return DoSomethingInternal();
}
internal abstract string DoSomethingInternal();
public abstract string DoSomethingExternal();
}
public class MyClass:MyAbsClass
{
internal override string DoSomethingInternal(){}
public override string DoSomethingExternal(){}
}
}
and now you have (lets say another consoleApp called ConsoleApplication2) you build your consoleapplication1 and add reference to it
using ConsoleApplication1;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
}
}
class NewClass : MyAbsClass
{
public override string DoSomethingExternal()
{
throw new NotImplementedException();
}
}
}
Now when you build this you get an error Error 1 'ConsoleApplication2.NewClass' does not implement inherited abstract member 'ConsoleApplication1.MyAbsClass.DoSomethingInternal()
Why this error?? because compiler is telling you that when you declare member as internal ,the class which is in the same assembly ie (ConsoleApplication1) can have access it.So no matter how many class you create in ConsoleApplication2 you wont be able to access it because it is declared internal.
The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly, as in this example
public class BaseClass
{
// Only accessible within the same assembly
internal static int x = 0;
}
Now in your case this is something like:
public abstract BaseClass
{
internal abstract void Print();
}
Now, in this case all the class which are in same assembly of BaseClass, will be able to override the Print method of BaseClass. But outside of the assembly classes will only be able to override public members. like
public abstract BaseClass
{
internal abstract void Print();
public abstract void Hello();
}
Classes outside of the assembly of BaseClass won't be able to use this class as one of member of this class is internal. Solution is create sub class in same assembly of BaseClass and use that class outside the assembly.
I suggest you to read the access modifier concept in c# from C# in depth by @john skeet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With