Friday, 12 December 2014

Object Oriented Programming access specifires

Access Specifiers in Object Oriented Programming.


Access specifiers defines the access rights for the statements or functions that follows it until another access specifier or till the end of a class. The three types of access specifiers are "private", "public", "protected".
private:
The members declared as "private" can be accessed only within the same class and not from outside the class.
public:
The members declared as "public" are accessible within the class as well as from outside the class.
protected:
The members declared as "protected" cannot be accessed from outside the class, but can be accessed from a derived class. This is used when inheritaance is applied to the members of a class.

Access modifiers
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can only be accessed by code in the same class or struct.
protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
Static
The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:
static class Foo()
{
    static Foo()
    {
        Bar = "fubar";
    }

    public static string Bar { get; set; }
}
Static classes are often used as services, you can use them like so:
MyStaticClass.ServiceMethod(...);

Public - If you can see the class, then you can see the method
Private - If you are part of the class, then you can see the method, otherwise not.
Protected - Same as Private, plus all descendants can also see the method.
Static (class) - Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.
Static (method) - Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.

We use these keywords to specify access levels for member variables, or for member functions (methods).
.Public variables, are variables that are visible to all classes.
.Private variables, are variables that are visible only to the class to which they belong.
.Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

Private

Like you'd think, only the class in which it is declared can see it.

Package Private

Can only be seen and used by the package in which it was declared. This is the default in Java (which some see as a mistake).

Protected

Package Private + can be seen by subclasses or package member.

Public

Everyone can see it

private
The type or member can only be accessed by code in the same class or struct.
protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.Static Modifier
Static methods are called without an instance reference.

static does not mean it is accessible everywhere. You still need protected/private to define visibility.



No comments:

Post a Comment