Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you choose, protected or internal?

If I have a class with a method I want protected and internal. I want that only derived classes in the assembly would be able to call it.

Since protected internal means protected or internal, you have to make a choice. What do you choose in this case - protected or internal?

like image 862
brickner Avatar asked May 20 '10 04:05

brickner


1 Answers

Personally I would choose protected. If subclasses in your own assembly are good enough to call the method, why wouldn't a subclass in another assembly? Perhaps you could refactor the functionality into a separate (internal) class altogether.

You really need to think objectively about the purpose of the method. Internal accessibility almost always feels wrong to me. Mostly because of my experience trying to derive from controls or classes in the .NET framework where I ran into a brick wall because someone decided to mark a class or method as internal. The original author never noticed that not having access to that method made things much harder to implement a subclass.

EDIT

To clarify, internal accessibility for a class is very useful and I wasn't implying internal in general is bad. My point was that internal methods on an otherwise public class seems wrong to me. A properly designed base class should not give an unfair advantage to derived classes in the same assembly.

like image 75
Josh Avatar answered Oct 10 '22 21:10

Josh