Friday, 31 May 2013

Access Modifiers Example

Case1. SameClass, SamePckage : All fields are accessble
package com.sample.javase.testing;

public class AccessModifiersExample {

public int i = 10;
private int j = 9;
protected int k = 8;
int l = 7;

public void publicMethod() {
System.out.println("this is public method");
}

private void privateMethod() {
System.out.println("This is private method");
}

protected void protectedMethod() {
System.out.println("this is protected method");
}

void noSpecifierMethod() {
System.out.println("This is no specifier method");
}

/**
 * @param args
 */
public static void main(String[] args) {

/**
 * Here we are trying to access all methods and variables in the same
 * class. So we can access everything like private , public, nomodifier
 * and proteced variables and methods.So no problem here.
 */

AccessModifiersExample accessModifiersExample = new 
  AccessModifiersExample();
accessModifiersExample.privateMethod();
accessModifiersExample.publicMethod();
accessModifiersExample.protectedMethod();
accessModifiersExample.noSpecifierMethod();
System.out.println("Value of i is " + accessModifiersExample.i);
System.out.println("Value of j is " + accessModifiersExample.j);
System.out.println("Value of k is " + accessModifiersExample.k);
System.out.println("Value of l is " + accessModifiersExample.l);
}
}

***********************************************************
Case2. Subclass, SamePckage : public, protected and nomodifiers fields are accessble and private fields are not accessble

package com.sample.javase.testing;

public class AceessModifiersExample1 extends AccessModifiersExample {

/**
 * @param args
 */
public static void main(String[] args) {

/**
 * This is subclass in the same package. So here we can
 * not access the private variabls and methods. Here we can access both
 * public, protected and nomodifier variables and methods.
 */
AccessModifiersExample1 accessModifiersExample = new 
  AccessModifiersExample1();
accessModifiersExample.protectedMethod();
accessModifiersExample.publicMethod();
accessModifiersExample.noSpecifierMethod();
// private method is not accessble out side the class
// accessModifiersExample.privateMethod();
System.out.println("value of i is " + accessModifiersExample.i);
System.out.println("value of k is " + accessModifiersExample.k);
System.out.println("value of l is " + accessModifiersExample.l);
}
}

***********************************************************

Case3. OtherClass, SamePckage : public, protected and nomodifiers fields are accessble and private fields are not accessble

package com.sample.javase.testing;

public class AceessModifiersExample11 {

/**
 * @param args
 */
public static void main(String[] args) {

/**
 * This is another class in the same package. So here we can
 * not access the private variabls and methods. Here we can access both
 * public, protected and nomodifier variables and methods.
 */
AccessModifiersExample accessModifiersExample = new 
  AccessModifiersExample();
accessModifiersExample.protectedMethod();
accessModifiersExample.publicMethod();
accessModifiersExample.noSpecifierMethod();
// private method is not accessble out side the class
// accessModifiersExample.privateMethod();
System.out.println("value of i is " + accessModifiersExample.i);
System.out.println("value of k is " + accessModifiersExample.k);
System.out.println("value of l is " + accessModifiersExample.l);
}
}

***********************************************************

Case4. Subclass, OtherPckage : only public and protected fields are accessble and private and nomodifier  fields are not accessble

package com.sample.javase.testing1;

import com.sample.javase.testing.AccessModifiersExample;

public class AccessModifiersExample2 extends AccessModifiersExample {

/**
 * @param args
 */
public static void main(String[] args) {

/**
 * This is subclass in the other package. So here we can
 * not access the private and nomodifier variabls and
 * methods. Here we can access only public and protected variables and   
   * methods.
 */
AccessModifiersExample2 accessModifiersExample = new 
  AccessModifiersExample2();
accessModifiersExample.publicMethod();
  accessModifiersExample.protectedMethod();
  // accessModifiersExample.noSpecifierMethod();
// private method is not accessble out side the class
// accessModifiersExample.privateMethod();
System.out.println("value of i is " + accessModifiersExample.i);
 // System.out.println("value of k is " + accessModifiersExample.k);
 // System.out.println("value of l is " + accessModifiersExample.l);
}
}

***********************************************************
Case5. OtherClass, OtherPckage : only public fields are accessible and private, protectded and nomodifier  fields are not accessble

package com.sample.javase.testing1;

import com.sample.javase.testing.AccessModifiersExample;

public class AccessModifiersExample22 {

/**
 * @param args
 */
public static void main(String[] args) {

/**
 * This is another class in the other package. So here we can
 * not access the private, protected and nomodifier variables and
 * methods. Here we can access only public variables and methods.
 */
AccessModifiersExample accessModifiersExample = new 
  AccessModifiersExample();
accessModifiersExample.publicMethod();
  // accessModifiersExample.protectedMethod();
  // accessModifiersExample.noSpecifierMethod();
// private method is not accessble out side the class
// accessModifiersExample.privateMethod();
System.out.println("value of i is " + accessModifiersExample.i);
  // System.out.println("value of k is " + accessModifiersExample.k);
  // System.out.println("value of l is " + accessModifiersExample.l);
}
}

No comments:

Post a Comment