Monday, 1 July 2013

Access modifiers

They are access modifiers and help us implement Encapsulation (or information hiding). They tell the compiler which other classes should have access to the field or method being defined.

Access modifiers can be apply to 
class, 
fields 
and methods. 

public :
private :
protected :
internal :
protected internal

Public - Any class can refer to the field or call the method. The type or member can be accessed by any other code in the same assembly or another assembly that references it.
The public keyword is an access modifier for types and type members. Public access is the most permissive access level.
There are no restrictions on accessing public members.
 Accessibility
Can be accessed by objects of the class

Can be accessed by derived classes

Private - Only the current class will have access to the field or method. This is the default access modifier type if none is formally specified. By default methods and fields are private. 
Accessibility
Cannot be accessed by object

Cannot be accessed by derived classes

Protected - Only the current class and sub classes (and sometimes also same-package classes) of this class will have access to the field or method. A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.
Accessibility
Cannot be accessed by object

Can be accessed by derived classes


public class A
    {
       protected int fieldA = 8;
    }
 
    public class B : A
    {
       public void MethodB()
       {
           A obja = new A();
           // obja.fieldA = 5; // Error
 
           B objb = new B();
           objb.fieldA = 23;
 
       }
 
    }


internalAccess is limited exclusively to classes defined within the current project assembly.  Internal member can b accessible anywhere whithin a project. By default Classes are internal

Class Modifiers:
A class can be…
public
private
internal
abstract
sealed
new

public: Instances of this class are available to any class that wants to access.
private: A nested class that can only be accessed inside the class in which it is defined.
internal: The class is only accessible from other classes in the same assembly. This is the default access for non-nested types. If no modifier is specified, the class has internal access.
abstract: An instance of the class cannot be created.Usually this means the class is intended to serve as a base class.
sealed: The class cannot serve as a base class for another class (it can’t be derived from). A class cannot be both abstract and sealed.
new: Used only with nested classes. “New” indicates that the class hides an inherited member of the same name.

Methods:
Methods can be declared as virtual, abstract , or sealed. Methods can be overloaded, overridden and hidden.
virtual methods can be overriden by a derived class using the override keyword.
abstract methods must be overriden in a derived class. If any method of a class is abstract, the entire class must be declared as abstract.

sealed methods are methods that override an inherited virtual method having the same signature. When a method is sealed, it cannot be overriden in a derived class.

No comments:

Post a Comment