Friday, February 1, 2013

Playing with Access Modifiers in JAVA

To make you understand the concepts i have created the whole project.. The project structure is as below.










// Demo class which the super class
package com.source;

public class Demo {                                                            

 private int a;
 public int b;
 protected int c;
 int d;
 
 public Demo() {
 System.out.println("This is a public demo constructor");
 }
 
 private Demo( String one ){
 
  System.out.println("This is a private one argument demo constructor");
 }
 
 private void method(){
  System.out.println("This is a private method");
 }
 public  void method1(){
  System.out.println("This is a public method1");
 }
 protected  void method2(){
  System.out.println("This is a protected method2");
 }
 void method3(){
  System.out.println("This is a defualt method");
 }
 
 
 // Playing with inner classes
 
 public static class PublicStaticInnerClassOfDemo{
  
  public void InnerMethod(){
   System.out.println("public static class *Hi I am inside an 
              innerMethod which is public");
  }
  
  public static void staticInnerMethod(){
   System.out.println("public static class *Hi I am inside an 
       innerMethod which is public static  ");
  }
  
  private static void staticInnerMethodPrivate(){
   System.out.println("public static class *Hi I am inside an 
       innerMethod which is private  static ");
  }
  protected static void staticInnerMethodProtected(){
   System.out.println("public static class *Hi I am inside an 
          innerMethod which is protected  static ");
  }
  
   static void staticInnerMethodDefault(){
   System.out.println("public static class *Hi I am inside an 
        innerMethod which is default  static ");
  }
 }
 
 protected class ProtectedInnerClassOfDemo{
  
  public void InnerMethodPublic(){
   System.out.println("ProtectedInnerClassOfDemo *Hi I am inside an 
              innerMethod which is public");
  }
  
  private  void InnerMethodPrivate(){
   System.out.println("ProtectedInnerClassOfDemo *Hi I am inside an 
                 innerMethod which is private   ");
  }
  
  protected  void InnerMethodProtected(){
   System.out.println("ProtectedInnerClassOfDemo *Hi I am inside an 
           innerMethod which is protected   ");
  }
  
   void InnerMethodDefault(){
   System.out.println("ProtectedInnerClassOfDemo *Hi I am inside an
                   innerMethod which is default   ");
  }
 }
 
  class DefaultInnerClassOfDemo{
  public void InnerMethodPublic(){
   System.out.println("DefaultInnerClassOfDemo *Hi I am inside an 
               innerMethod which is public");
  }
  
  private  void InnerMethodPrivate(){
   System.out.println("DefaultInnerClassOfDemo *Hi I am inside an 
            innerMethod which is private   ");
  }
  
  protected  void InnerMethodProtected(){
   System.out.println("DefaultInnerClassOfDemo *Hi I am inside an 
              innerMethod which is protected   ");
  }
  
   void InnerMethodDefault(){
   System.out.println("DefaultInnerClassOfDemo *Hi I am inside an 
               innerMethod which is default   ");
  }
 }
 
 private class PrivateInnerClassOfDemo{
  // you can't extend private inner class of Demo
 }
 
   public  class PublicInnerClassOfDemo{
  
    /*The method innerMethod1 cannot be declared 
    static; static methods can only be declared in a 
    static or top level type*/
 //   public static void innerMethod1(){}
  
    public void InnerMethodPublic(){
   System.out.println("PublicInnerClassOfDemo *Hi I am inside an 
         innerMethod which is public");
  }
  
  private  void InnerMethodPrivate(){
   System.out.println("PublicInnerClassOfDemo *Hi I am inside an 
        innerMethod which is private   ");
  }
  
  protected  void InnerMethodProtected(){
   System.out.println("PublicInnerClassOfDemo *Hi I am inside an 
        innerMethod which is protected   ");
  }
  
   void InnerMethodDefault(){
   System.out.println("PublicInnerClassOfDemo *Hi I am inside an 
       innerMethod which is default   ");
  }
  
  
 }

}

=============================END OF DEMO CLASS ======================

// class which extends Demo class in the same package and will tell which
of the methods and variables can be accessed.

public class DemoInSamePackage extends Demo{

 
 public DemoInSamePackage() {
 System.out.println("DemoInSamePackage is called");
 }
 
 public  void mySamePackageMethod(){
  System.out.println("MySamePackage public method is accessing 
                     params of super class's variables");

  System.out.println("a === private but not allowed");
  System.out.println("b === public allowed "+b);
  System.out.println("c === pretected allowed"+c);
  System.out.println("d === default "+d);
  
  callingMethods();
 }

 private void callingMethods() {
 System.out.println("\n MySamePackage private method is 
                 accessing methods of super class");

 System.out.println("Not allowed to call the 
        private method of the super class");

 method1();
 method2();
 method3();
 
 }
}
=========================END OF THE CLASS=======================

// class that extends inner classes of Demo class 
       and tells which vars and methos can be accessible


public class ExtendInnerClassInSamePackage extends Demo.PublicStaticInnerClassOfDemo{

// When extending Demo.PublicStaticInnerClassOfDemo
 public void method(){

 Demo.PublicStaticInnerClassOfDemo.staticInnerMethod();    // ok 
 Demo.PublicStaticInnerClassOfDemo.staticInnerMethodDefault(); //ok
 Demo.PublicStaticInnerClassOfDemo.staticInnerMethodProtected(); //ok
  
 }
 
 /*When extending Demo.ProtectedInnerClassOfDemo*/
}



// Important 
/* 
 *  you can't extend the other classes but if you want you can do like below
  public class ExtendInnerClassInSamePackage extends Demo{
 class A extends ProtectedInnerClassOfDemo{ 
    public void  method(){
    Demo d = new Demo();
    Demo.PublicInnerClassOfDemo p = d.new PublicInnerClassOfDemo();
    p.InnerMethodPublic(); // ok
    p.InnerMethodProtected(); // ok 
    p.InnerMethodDefault(); // ok
     }
 }
 like this you can call all the classes but private class 
          and private methods of the classes you can't call 
 * 
 * */
=============================END OF CLASS===========================

// Main class that creates objects of classes

package com.source;

import com.sourcebits.DemoInOtherPackage;
import com.sourcebits.ExtendInnerClassInOtherPackage;

public class Main {
 
 public static void main( String [] args ){
  
   
 Demo d = new Demo();
  
 // you can't not call private constructor of 
             a class but default is ok
  // Demo privateConstructor = new Demo("hi");
  
 System.out.println("========Demo class accessing its own variables ");
 System.out.println("a === private not allowed "); 
//but if object is created inside the Demo class then it 
         can access private variable 
  
 System.out.println("b === public allowed "+d.b);
 System.out.println("c === protected allowed "+d.c);
 System.out.println("d === default allowed "+d.d);
  
 System.out.println("========Demo class accessing its own Methods ");
  
// d.method(); not allowed but if object is created inside the Demo 
          class then it can access private methods 

  d.method1(); // public method ok
  d.method2(); // protected ok 
  d.method3(); // defualt ok
  
  System.out.println("\n");
  System.out.println("When calls are from same package");
  DemoInSamePackage samePackageObj = new DemoInSamePackage();

samePackageObj.mySamePackageMethod(); // but if the method were private 
                                         could not have been accessed
  
 System.out.println("\n");
 System.out.println("When calls are from other package\n\n ");

 DemoInOtherPackage otherPackageObj =  new DemoInOtherPackage();
 otherPackageObj.myOtherPackageMethod();
  
      
System.out.println("When calls are from Inner classes which is in same package");

 ExtendInnerClassInSamePackage innersame = new ExtendInnerClassInSamePackage();
   innersame.method();
  innersame.InnerMethod(); // ok

  ExtendInnerClassInSamePackage.staticInnerMethod();  // ok
  ExtendInnerClassInSamePackage.staticInnerMethodDefault(); // ok
  ExtendInnerClassInSamePackage.staticInnerMethodProtected(); // ok

  
 System.out.println("When calls are from Inner classes which is in Other package");
       
 ExtendInnerClassInOtherPackage innerother = new ExtendInnerClassInOtherPackage();
   innerother.InnerMethod();
  innerother.method();

     ExtendInnerClassInOtherPackage.staticInnerMethod();  // ok
     ExtendInnerClassInOtherPackage.staticInnerMethodProtected(); //Ok
     
  
 }

}
===============================END OF CLASS==========================



/* class extends Demo   that tells if we can access vars and methods of Demo class*/

package com.sourcebits;

import com.source.Demo;

public class DemoInOtherPackage extends Demo{

 public DemoInOtherPackage() {
 System.out.println("DemoInOtherPackage default arguement constructor");
 }
 
 public void myOtherPackageMethod(){

  System.out.println("MyOtherPackage public method is 
                                    accessing variables of super class");
 
  System.out.println("a ===  private not allowed");
  System.out.println("b === public allowed "+b);
  System.out.println("c === protected allowed"+c);
  System.out.println("d === defalut not allowed");
  
  callingMethods();
 }
 private void callingMethods() {
  System.out.println("MyOtherPackage private method is 
                             accessing methods of super class");

System.out.println("Not allowed to call the private method of the super class");

  method1();
  method2();

System.out.println("Not allowed to call the default method of the super class");
  
  }
}
=============================END OF CLASS============================


// class that extends inner class of Demo class

package com.sourcebits;
import com.source.Demo;


public class ExtendInnerClassInOtherPackage extends Demo.PublicStaticInnerClassOfDemo{

// When extending Demo.PublicStaticInnerClassOfDemo
 public void method(){

Demo.PublicStaticInnerClassOfDemo.staticInnerMethod(); // ok
  
// ** Demo.PublicStaticInnerClassOfDemo.staticInnerMethodDefault();  // not allowed
  
Demo.PublicStaticInnerClassOfDemo.staticInnerMethodProtected(); //ok
  
 }
 
}





//Important 
/* 
 * you can't extend the other classes but if you want you can do like below

 public class ExtendInnerClassInOtherPackage extends Demo{

  class A extends PublicInnerClassOfDemo{ 

   public void  method(){

 Demo d = new Demo();
     Demo.PublicInnerClassOfDemo p = d.new PublicInnerClassOfDemo();

      
                   p.InnerMethodPublic(); // ok
                   InnerMethodProtected(); //  allowed 
     p.InnerMethodProtected(); // not allowed 
     p.InnerMethodDefault(); // not allowed 

     }
 }

NOTE : In the different package you can't extend  
         default,private class but public
 * 

when extending protected class :
public class ExtendInnerClassInOtherPackage extends Demo{

  class A extends ProtectedInnerClassOfDemo{ 

     public void  method(){

 //Demo d = new Demo();
       //Demo.ProtectedInnerClassOfDemo p = d.new Prote(); // can’t create object 

   InnerMethodPublic(); // ok
   InnerMethodProtected(); // ok
   InnerMethodDefault(); // not allowed 

     }
 }

 * */
 
=====================END OF THE CONCEPT=============================

No comments:

Post a Comment