Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Security and object oriented programming..

  1. #11
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    A protected variable can only be read but not changed
    Public is open to anyone, protected is open to members of the same package, and private is only accessible from the same class.
    Both of you have your conceptions of protected mixed up. Java in fact defines 4 and not 3 access specifiers, public, protected, private and default or package private. Public members are open to all objects, private members to only the object itself (or "this" in java) and protected members to this and objects of derived classes. The default access specifier is package private, which means that it's open to access by classes in the same package.

    Sorry in advance for the length of the code.

    Code:
    package p;
    
    class c
    {
    public int i;
    protected int j;
    private int k;
    int l; //this is package private
    
    c()
    {
    //if no object name is given before the variable, this. is assumed
    i=2; //works
    j=2; //works
    k=2; //works
    l=2; //works
    }
    }
    
    class d extends c //d inherits i,j and l from class c
    {
    d()
    {
    i=2; //works
    j=2; //works
    k=2;//violates encapsulation and therefore generates a compile time error
    l=2; //works
    }
    }
    
    class e
    {
    c c1;  //object of class c
    d d1; //object of class d
    e()
    {
    c1=new c();
    d1=new d();
    
    e()
    {
    c1.i=0; //works
    c1.j=1; //violates encapsulation and therefore generates a compile time error
    c1.k=0; //violates encapsulation and therefore generates a compile time error
    c1.l=4; //works
    
    d1.i=0; //works
    d1.j=0; //violates encapsulation and therefore generates a compile time error
    d1.k=0;//member k was private in class c and therefore not inherited by d, generates a compile time error
    d1.l=2;//works
    }
    }
    In another package if we were to try to change l, the compiler would generate an error as it is package private.

    This fourth access specifier (package private) is an addition to java from C++.
    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  2. #12
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Just didn't write about it in my post. That's why I included a link there for him to read up about it.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •