Tag Archives: Implementing encapsulation

java – Access Modifiers

The communication between the classes,variables,methods and constructors are defined by the Access Modifiers.There are types in Access Modifiers.They are,

  • public
  • protected
  • private
  • default
Access Modifier
Access Modifier

Public:

The members with public access modifier are accessible by any class or member of class in java.These public variables,methods,classes and constructors are visible to all classes in java.

Protected:

The protected variables and methods cannot be inherited by interface and classes.It cannot be used as modifiers to methods and variables inside the interface.These members can be accessed by the sub-class of different package and classes of same package.

Private:

The private variables and methods cannot be inherited by interface and classes.It cannot be used as modifiers to methods and variables inside the interface Private members are only accessible inside the class in which it is declared.

Note: Best strategy to access the private members is by using Getter and Setter methods.It is called as Encapsulation


Default:

When no access modifier is specified to a member,it will be considered as default access modifier member.The member with default access modifier can be accessed by Sub-Class of Same package or Classes of Same Package but not outside the package.

Example Code 1 – Sub Class within Same Package

  • Class: ModifierSetter.java


package p1;
public class ModifierSetter
{
public int a=100;
protected int b=200;
private int c=300;
int d=10;

public void setA(int a)
{
this.a = a;
}
public int getA()
{
return a;
}
private void setC(int c)
{
this.c = c;
}
private int getC()
{
return c;
}

protected void setB(int b)
{
this.b = b;
}
protected int getB()
{
return b;
}
void setD(int d)
{
this.d = d;
}

int getD()
{
return d;
}
}

  • Class: SubClassinSamePackage

  • package p1;
    public class SubClassinSamePackage extends ModifierSetter
    {
    public static void main(String args[])
    {
    ModifierSetter obj = new ModifierSetter();

    System.out.println("Value of A:"+obj.a);
    System.out.println("Value of B:"+obj.b);
    System.out.println("Value of D:"+obj.d);
    obj.setA(10);
    obj.setB(20);
    obj.setD(40);

    System.out.println("Value of A after Set:"+obj.getA());
    System.out.println("Value of B after Set:"+obj.getB());
    System.out.println("Value of D after Set:"+obj.getD());

    /*
    Below Lines raise error as we are trying to access private members
    */
    //System.out.println("Value of C:"+obj.c);
    //obj.setC(30);
    //System.out.println("Value of C after Set:"+obj.getC());
    }
    }

    Output:

    Value of A:100
    Value of B:200
    Value of D:10
    Value of A after Set:10
    Value of B after Set:20
    Value of D after Set:40

    Example Code 2 – Sub Class in Different Package

  • Class: ModifierSetter.java
  • package p1;
    public class ModifierSetter
    {
    public int a=100;
    protected int b=200;
    private int c=300;
    int d=10;

    public void setA(int a)
    {
    this.a = a;
    }
    public int getA()
    {
    return a;
    }
    private void setC(int c)
    {
    this.c = c;
    }
    private int getC()
    {
    return c;
    }
    protected void setB(int b)
    {
    this.b = b;
    }
    protected int getB()
    {
    return b;
    }
    void setD(int d)
    {
    this.d = d;
    }
    int getD()
    {
    return d;
    }
    }

  • Class: SubClassinDifferentPacakage.java

  • package p1;
    public class SubClassinDifferentPacakage
    {
    public static void main(String args[])
    {
    ModifierSetter obj = new ModifierSetter();

    System.out.println("Value of A:"+obj.a);
    System.out.println("Value of B:"+obj.b);
    System.out.println("Value of D:"+obj.d);
    obj.setA(10);
    obj.setB(20);
    obj.setD(40);

    System.out.println("Value of A after Set:"+obj.getA());
    System.out.println("Value of B after Set:"+obj.getB());
    System.out.println("Value of D after Set:"+obj.getD());
    /*
    * Below Lines raise error as we are trying to access private members
    //System.out.println("Value of C:"+obj.c);
    //obj.setC(30);
    //System.out.println("Value of C after Set:"+obj.getC());

    }
    }

    Output:

    Value of A:100
    Value of B:200
    Value of D:10
    Value of A after Set:10
    Value of B after Set:20
    Value of D after Set:40

    Example Code 3 – Super Class from Different Pacakage

  • Class: ModifierSetter.java

  • package p1;
    public class ModifierSetter
    {
    public int a=100;
    protected int b=200;
    private int c=300;
    int d=10;

    public void setA(int a)
    {
    this.a = a;
    }
    public int getA()
    {
    return a;
    }
    private void setC(int c)
    {
    this.c = c;
    }
    private int getC()
    {
    return c;
    }
    protected void setB(int b)
    {
    this.b = b;
    }
    protected int getB()
    {
    return b;
    }
    void setD(int d)
    {
    this.d = d;
    }
    int getD()
    {
    return d;
    }
    }

  • Class: ClassInDifferentPackage.java

  • package p2;
    import p1.ModifierSetter;
    public class ClassInDifferentPackage
    {
    public static void main(String args[])
    {
    ModifierSetter obj = new ModifierSetter();
    System.out.println("Value of A:"+obj.a);
    obj.setA(10);

    /*
    * Below Lines raise error(private and protected members are not accessible by a Super class from different package)
    */
    // System.out.println("Value of b:"+obj.b);
    // System.out.println("Value of D:"+obj.d);
    // System.out.println("Value of c:"+obj.c);
    // obj.setB(20);
    // obj.setD(40);
    // obj.setC(30);
    // System.out.println("Value of c after Set:"+obj.getC());
    // System.out.println("Value of B after Set:"+obj.getB());
    // System.out.println("Value of D after Set:"+obj.getD());
    }
    }

    Output:

    Value of A:100