Wednesday, October 20, 2010

Access Specifiers in Java

Point-1 : Four types of Access Specifiers in Java
                    1. Public 2.Private 3.Protected 4.Default.

Note1 : We can get access to the private members of a class through the public
 members of the same class from other class.

Note2 : We cannot access the private members of a super class from the objects
 of the sub-Class.

Question : What is the Difference between the Public and Default ?
Answer   :  Default specifier is public as long as control in under same package and it is
 private if it access from outside of the package.
                 Simply, we cannot access the default class from outside of it's package.
Example :
                        package -Name : mypackage
                        class -     Name : First.java
               Code :
                          package myPackage;

  class First {
public int i;
public void function(){
System.out.println(" Function in Class First in Package 'myPackage'");
}
}
and another Class Another.java in Different package.
                      Package Name : newPackage.
                      Class Name    :   Another.java
          Code :
                    package newPackage;
                    import myPackage.*;

public class Another {
public static void main(String[] args)
        {
First f = new First();
 /*Error We cannot access the Class First because it is default,
 only "public" can access like */
}
}
Question : What is the Access Specifier for the Default Constructor in Java ?
Ans : It is public, so that we can create object for the class even in another package.

Question : Can a Constructor be Static ?
Ans : NO , constructor cannot be static.

Question : Can we create object of the System  class ?
Ans : No, we cant because all the constructors in the system class in private.

Question : If we cannot create an Object of System class, then how come we access
 the non-Static members of the System Class ?
Answer : Except constructor in the System Class, remaining all the methods and variables
 are static [so that we can access static members with class name]

Question: How to access the non static members of a class whose constructor
               declared as private?
Answer :  Since the Constructor declares as private, we cannot create a Object, so we
 cannot access the non-static member of the class,in that cases we need an Object of that class.
                       ?????????????? how to get that ????????????
Solution :
       Create a public static function in that class, where it returns the object of that class.

      WOW!!!!!, so what is this Method/function called as Factory Methods.

Question : Give me an Example for factory method.

  package myPackage;

  class First {

public int i;

private First(){
System.out.println(" Private Constructor In Package myPackage and class First");
}
public void function(){
System.out.println(" Function in Class First in Package 'myPackage'");
}

/*
 * Factory Method
 */
public static First returnObject(){
First f = new First();
return f;
}
}
  class Second {
 public int k;
 public static void main(String args[]){
 First f = First.returnObject();
 f.i = 500;
 System.out.println("Accessing a Non Static variable using Factory Mehod and it value "+f.i);
 }
  }
 Question :can we create a Constructor as private ?
Answer : we cannot create the Constructor of class as private,if you want to create object
 of that class in another class.
Example :
                package mypackage
                class First
               {
                   private int i;
                 private First()
                {
                  Syso("Constructor of the Class First");
                }
              }
               class Second()
              {
                  private int j;
                   First f = new First();//ERROR : we cannot create the object,as Constructor is private
                   f.i ; //Private variables cannot be accessed outside of the class.
               }
            
Note : Access Specifiers not applicable to Local Variables, because their scope exists to
           method/function in which they exist.
Note : Access Specifiers are not applicable to Local Variables.

Question : can i Declare a Java Class as "private" , "static" or Protected?
Answer   : we cannot declare a java class as private/static/protected only we can declare
class as :
               public/default(no declaration)/final OR Abstract.

Question : can i declare a java Inner class as "private" / static / protected?
Answer : Yes, we can declare inner class of java with above access specifiers.

Question : Explain Something about the Access Specifier Protected?
Ans:  The Protected feature of class (means any protected variable ) is available to all
          classes in the same package.
                           (And)
   Moreover, a protected feature is available to all subclasses of the class that own 
  the protected feature even the sub-classes present in the different package.


Note:  Sub-classes can access the protected members of it's super class only on the
 objects of the it's class, not on objects of it's super class.
Example: 
               class Child extends Parent
{
       void accessMethod(Parent p. Child c)
{
     p.protectedVariable = 20; /* ERROR */
     p.protectedMethod();   /* ERROR */
     c.protectedVariable = 40;//Legal
     c.protectedMethod();  //Legal.
}
}

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...