This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 3 years ago.
I am new to C# programming and I have some questions about access modifier protected and private .
Is it true that when data members and member functions of the class are kept "private" ,they can only be accessed by that class and can't even be accessed by the child (derived) class of that class?
In case of protected, if a data members and member functions of that class are kept protected ,then only the code of that class can access those data members and member functions and also if that class has a derived(child ) class ,then even child class can also access the protected data members and member functions of that base (parent ) class ?
Thank
Protected members are accessible by subclasses, private are accessible only in the class
Well this is going to be a short answer but : yes, this is correct.
Related
This question already has answers here:
Default visibility for C# classes and members (fields, methods, etc.)?
(4 answers)
Closed 9 years ago.
Inside a class by default everything is private.
By default non nested class ,interface, struct, delegate & enum have internal accessibility.
But that means if all these comes inside class, everything will become private by default.
Are there any types that will become non private inside a class by default?
PS. just a kind of exceptional case like an instance variable that we can't assign any values inside a struct, but by using null coalescing operator we can assign.
Please have a read of http://msdn.microsoft.com/en-us/library/ms173121.aspx
Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
(emphasis mine).
This question already has answers here:
Why can't my public class extend an internal class?
(5 answers)
Closed 9 years ago.
See this code:
internal class c
{
private int d;
}
public class a : c
{
private int b;
}
Why can I not inherit a public class from an internal class? Why does the compiler have this behavior?
Because the public class would be visible outside your current assembly, while the internal one isn't. When deriving from a class you can only restrict visibility further, because in your case it would make the implementation of c available to consumers outside your assembly which kind of defeats the purpose of making the class internal in the first place.
You can use composition instead of inheritance, though.
C# design principle. Derived class should atleast have same accessibility as the parent class. In your case it is not hence not allowed. Take a look at Eric Lippert's view on this deriving public class from an internal class
Because "public class" is more "visible" than "internal class".
C# language has visibility protection layer that prevents this.
Internal classes can only be accessed from within the Assembly in which they are defined. When public class a inherits from an internal class in effect attempts to make the internal class public.
To avoid this encapsulate the internal class in the public class.
This question already has answers here:
Is it possible to have a private class?
(4 answers)
Closed 9 years ago.
Whats the difference between "Class" only and "Private Class" declaration in C#?
If these are nested classes, there's no difference:
namespace Foo
{
public class Outer
{
private class ExplicitlyPrivate {}
class ImplicitlyPrivate {}
}
}
Type members always default to being private.
If it's a top-level class, then you can't make it private - but the default is internal:
namespace Foo
{
class ClassIsInternalByDefault {}
}
When you declare a class without specifying an accessibility modifier it will default to the lowest accessibility possible.
More practically, specifying private when private is not permissible can result in a compilation error.
A simple answer is to say that a private class is meant to protect attributes within that class from being changed by any external classes, other than during construction of the program. A normal "class", well, doesn't have that protection.
It is a form of ENCAPSULATION.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between ‘protected’ and ‘protected internal’?
I have seen a lot of controversy over the true meaning of declaring a member protected internal.
Under this context is the member's access modifier either "protected or internal" or "protected and internal"?
The documentation is clear that it is "protected or internal".
That is - a member is accessible both within the assembly and any subtype.
protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why can't I have protected interface members?
as title, in C#. Is there no possibility that someone might want to have a protected or an internal interface?
Because Interface is in crude terms 'a view to the outside world' and since it is for the outside world, there is no point making its members protected or private.
Or in other words, it is a contract with the outside world which specifies that class implementing this interface does a certain set of things. So, hiding some part of it doesn't make sense.
However, interfaces themselves can have access specifiers like protected or internal etc. Thus limiting 'the outside world' to a subset of 'the whole outside world'.
Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.
All the interface methods are Public. You can't create an access modifier in interface. If you want to use one, use Abstract class.
This is due to the nature of the interface. An interface, by definition is a specification.
A rule in .NET specifications dictates that a class that implements an interface will have to implement all members of that interface.
Now if we mark a member private, then the implementing class cannot implement that particular member.
Please see Non Public Members for C# Interfaces
Interfaces are Coding contracts, this is the very reason it won't allow any access modifier other then Public in it's Method signatures.
But an Interface by itself can be Internal but not private or protected, Internal allows access within the assembly which is perfectly fine.