Tuesday, 21 January 2014

Introducing access controls in java

how to specify access specifiers: public: private: protect

Access control:


 Encapsulation links data with the code that manipulates it. However,encapsulation provides another important attribute: access control.Java’s access specifiers are public, private, and protected. Java also defines a default access level.protected applies only when inheritance is involved. When a member of a class is modified by the public specifier, then that member can be accessed by any other code.
When a member of a class is specified asprivate, then that member can only be accessed by other members of its class. 

Acess specifiers

public:

A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java 
Universe.

private:

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private.Variables that are declared private can be accessed outside the class if public getter methods are present in the class.

protect:

Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The protected access modifier cannot be applied to class and interfaces.

Default:

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.

Example for access control

/* This program demonstrates the difference between public and private.*/
class Test 
{
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i)  // set c's value
{
c = i;
}
int getc()  // get c's value
{
return c;
}
}
class AccessTest 
{
public static void main(String args[]) 
{
Test ob = new Test();
// These are OK, a and b may be accessed directly
ob.a = 10;
ob.b = 20;
// This is not OK and will cause an error
// ob.c = 100; // Error!
// You must access c through its methods
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}










































3 comments :

  1. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.

    software testing course in chennai

    ReplyDelete
  2. You lost me, buddy. I mean, I suppose I get what youre saying. I have an understanding of what you’re saying, but you just appear to have ignored that there are some other individuals within the world who see this issue for what it really is and may not agree with you. You may perhaps be turning away a lot of persons who may have been followers of your website. javascript blogging platform

    ReplyDelete
  3. I just added this weblog to my rss reader, great stuff. Cannot get enough! python string comparison

    ReplyDelete