User:Speri: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(Created page with "[Riverside]")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[[Riverside, California|Riverside]]]
'''Access Control  in Object Oriented Languages'''
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages.
=='''Access Control'''==
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].
 
==Access Control in Ruby==
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .
*''private''  :The methods that are declared as private can only be invoked in the context of the current object.
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.
 
In Ruby these modifiers only apply to the methods but not the instance variables.
 
====Public Methods in Ruby====
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.
Declaration of  public methods:
 
<pre>     
class A             
  def method   
    puts "hello"   
  end             
end         
</pre>
 
<pre>     
class A             
  def method   
    puts "hello"   
  end
  public :method             
end             
</pre>
 
====Private Methods in Ruby====
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.
 
======Declaration of Private methods======
<pre>
class A
private 
  def method
  end
end
 
class A
  def method
  end
  private :method
end
</pre>
 
An example to show that private methods in Ruby can only be invoked by the current object in context.
<pre>
class A
  def method
    puts "I am a private method"
  end
  def pubm
    method
    A.new.method                    // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can
    puts "I am a public  method"        invoke the private method but not the new object.
  end
  private :method
end
 
class B
  def m1
    method                        //private methods of the base class can be accessed by the sub class
  end
end
a=A.new
b=B.new
a.pubm
a.method                          //gives an error
b.m1
</pre>
 
===Protected Methods in Ruby===
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).
 
Declaration of protected methods is ruby.
<pre>
class A
protected
  def method
  end
end
 
class A
  def method
  end
protected :method
end
</pre>
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.
 
<pre>
class A
  def method
    puts "I am a protected method"
  end
  def pubm
    method
    A.new.method                    // It does not give an error because in the context of the current object, another object belonging to same
    puts "I am a public  method"        class can invoke the protected method .
  end
  protected :method
end
 
class B
  def m1
    method                        //protected methods of the base class can be accessed by the sub class.
  end
end
a=A.new
b=B.new
a.pubm
a.method                          //gives an error
b.m1
</pre>
 
 
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.
<pre>
class A
  def method
    puts "I am a private method"
  end
  def pubm
    method
    A.new.method                    // It does not give an error because in the context of the current object, another object belonging to same
    puts "I am a public  method"        class can invoke the protected method .
  end
  private :method
end
a=A.new
a.method                // gives an error
a.send(:method)        // prints "I am a private method"
a.send :method          // prints "I am a protected method"
</pre>
==Access Control in C++==
 
===Controlling Access to the Member Data and Member Functions===
 
In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected.
A constructor should never be declared as private.
Access control is applied to all the names uniformly.
 
===Controlling the Level of accessibility of Base class and its Members===
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.
 
<pre>
class A
{
      int a;
      public:
            int value()
            {
                a=10;
                return a;
            }
};
class B:public A
{
      public:   
      int b;
      void bvalue()
      {
          b=value();    //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member
          printf("%d",b);  variables       
      }
};
  int main()
  {
    B o;
    o.bvalue();
    return 1;
  }
</pre>
 
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.
 
 
===Friendly Functions===
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.
 
===Friend Classes===
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.
 
==Access Control in Java==
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints.
Java provides two levels of access controls
 
*"Top Level" – Public, or Package-Private (no explicit modifier, also known as default).
 
*"Member Level" – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).
===Top Level===
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.
===Member Level===
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages.
 
 
====Example====
To understand it in more detail consider the following the example.
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –
Class A – It can access all its members independent of their access modifier.
Class B – It can access all the members of class A except those are private.
Class C – It can access public and protected members of class A because it is a sub class of class A.
Class D – It can access only the public members of class A.
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages.
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.
 
==Access Control in C#==
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.
 
===Accessibility levels provided by C#===
 
*"Public": It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.
*"Private": It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared.
*"Protected": It is a member access modifier. It allows the class to hide its members from the other classes except the child classes.
*"Internal": It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.
*"Protected Internal": It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.
[[File:Access2.png|400px|center|upright=0.56]]
 
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member.
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.
 
 
==References==
# http://ruby-doc.org/core/
# http://rubylearning.com/satishtalim/ruby_access_control.html
# http://phrogz.net/programmingruby/tut_classes.html
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx
# http://www.kuzbass.ru:8086/docs/isocpp/access.html
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control

Latest revision as of 23:52, 22 September 2011

Access Control in Object Oriented Languages

Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages.

Access Control

Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called Encapsulation.

Access Control in Ruby

Access control in ruby is achieved using the following access modifiersfor the methods.

  • public :The methods that are declared as public can be accessed by any object belong to the class .
  • private :The methods that are declared as private can only be invoked in the context of the current object.
  • protected :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.

In Ruby these modifiers only apply to the methods but not the instance variables.

Public Methods in Ruby

Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes. Declaration of public methods:

      
class A              
  def method     
    puts "hello"    
  end               
end          
      
class A              
  def method     
    puts "hello"    
  end 
  public :method              
end               

Private Methods in Ruby

private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods. In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.

Declaration of Private methods
class A 
 private  
   def method
   end
end

class A
   def method
   end
  private :method
end

An example to show that private methods in Ruby can only be invoked by the current object in context.

class A
  def method
    puts "I am a private method"
  end
  def pubm
    method
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can 
    puts "I am a public  method"        invoke the private method but not the new object. 
  end
  private :method
end

class B
  def m1
     method                         //private methods of the base class can be accessed by the sub class
  end
end
a=A.new
b=B.new
a.pubm
a.method                           //gives an error
b.m1

Protected Methods in Ruby

protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).

Declaration of protected methods is ruby.

class A
 protected
  def method
  end
end

class A
  def method
  end
 protected :method
end

Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.

class A
  def method
    puts "I am a protected method"
  end
  def pubm
    method
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same 
    puts "I am a public  method"        class can invoke the protected method .
  end
  protected :method
end

class B
  def m1
     method                         //protected methods of the base class can be accessed by the sub class.
  end
end
a=A.new
b=B.new
a.pubm
a.method                           //gives an error
b.m1


In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.

class A
  def method
    puts "I am a private method"
  end
  def pubm
    method
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same 
    puts "I am a public  method"        class can invoke the protected method .
  end
  private :method
end
a=A.new
a.method                // gives an error
a.send(:method)         // prints "I am a private method"
a.send :method          // prints "I am a protected method"

Access Control in C++

Controlling Access to the Member Data and Member Functions

In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected. A constructor should never be declared as private. Access control is applied to all the names uniformly.

Controlling the Level of accessibility of Base class and its Members

If a class is inheritedusing a public access specifier than the public members of the base class are inherited as the public members of the derived class and protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.

Diagram Depicting the Visibility of members and the Inheritance effect.
Diagram Depicting the Visibility of members and the Inheritance effect.

However, the sub-class members can determine the values of the base class private members by using the Base class public member functions.

class A
{
      int a;
      public:
             int value()
             {
                 a=10;
                 return a;
             }
};
class B:public A
{
      public:     
      int b;
      void bvalue()
      {
           b=value();     //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member 
           printf("%d",b);  variables        
      }
};
  int main()
  {
    B o;
    o.bvalue();
    return 1;
  }

Dynamic Binding can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.


Friendly Functions

Like send method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or :: scope resolution operator.Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.

Friend Classes

A class declared as a friend class of an another class can access all the private and protected members of the class extending friendship.

Access Control in Java

Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. Java provides two levels of access controls

  • "Top Level" – Public, or Package-Private (no explicit modifier, also known as default).
  • "Member Level" – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).

Top Level

On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.

Member Level

At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages.


Example

To understand it in more detail consider the following the example. In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like – Class A – It can access all its members independent of their access modifier. Class B – It can access all the members of class A except those are private. Class C – It can access public and protected members of class A because it is a sub class of class A. Class D – It can access only the public members of class A. The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.

Access Control in C#

The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.

Accessibility levels provided by C#

  • "Public": It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.
  • "Private": It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared.
  • "Protected": It is a member access modifier. It allows the class to hide its members from the other classes except the child classes.
  • "Internal": It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.
  • "Protected Internal": It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.

Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member.

  • - Assembly is a physical unit that can be executed, deployed, versioned and secured.


References

  1. http://ruby-doc.org/core/
  2. http://rubylearning.com/satishtalim/ruby_access_control.html
  3. http://phrogz.net/programmingruby/tut_classes.html
  4. http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx
  5. http://www.kuzbass.ru:8086/docs/isocpp/access.html
  6. http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm
  7. http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
  8. http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx
  9. http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control