You are designing a class for an application. You need to restrict the availability of the member variable accessCount to the base class and to any classes that are derived from the base class. Which access modifier should you use?
Assuming I've understood the question properly (English is not my native language) you are right. It has to be protected because a private variable could only be accesed in the class where it is declared.
I think this is a TRICKY question. Read carefully.. to "the" base class and to any classes that are derived from "the" base class. ..doesn't mean the one that we are designing is the base class, but our class is derived from certain base class along with another class that derived from that class to, so thats why it's a "private"
to anyone reading this, please ignore this comment, it's not correct. Private members are not accessible in derived classes.
You can test this really easily by creating a simple test C# console app with a base and derived class and seeing what you are able to access from the derived class:
public class MyBaseClass
{
private bool _thePrivateField = true;
protected bool _theProtectedField = true;
}
public class MyChildClass : MyBaseClass
{
public void TheMethod()
{
bool tryingToAccessThePrivateField = base._thePrivateField;
bool tryingToAccessTheProtectedField = base._theProtectedField;
}
}
If you do this you'll see a red squiggly line under 'base._thePrivateField' in 'MyMethod', because it's not accessible
The answer should be protected:
A protected member is accessible within its class and by derived class instances.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected
A private member ( i ) is only accessible within the same class as it is declared. A member with no access modifier ( j ) is only accessible within classes in the same package. A protected member ( k ) is accessible within all classes in the same package and within subclasses in other packages.
What you're thinking of is 'protected internal', which differs quite a lot from the default protected behaviour:
A protected internal member is accessible from the current assembly or from types that are derived from the containing class.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected-internal
upvoted 3 times
...
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Django
Highly Voted 3 years, 3 months agoJose
2 years, 11 months agoBoruc
Highly Voted 2 years, 1 month agoOryza
Most Recent 3 weeks, 3 days agoThooom
5 months, 2 weeks agoAishwaryaGupta
1 year agomatt11
11 months, 2 weeks agoqweasdcde
1 year, 3 months agoSamarHussain
1 year, 8 months agoPomphard
1 year, 9 months agoDamZ
1 year, 10 months agoPomphard
1 year, 9 months ago