Wednesday, 11 March 2015

Java Access Specifiers

Java Access Specifiers

The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.
Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no accessibility modifier is specified.
Access Modifiers
1.private
2.protected
3.default
4. public

public access modifier: Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

private access modifier: The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

protected access modifier: The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

default access modifier: Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.

Programming Analysis with respective Object
Java is a pure Object Oriented Language(OOL) so everything should be with real-time in the programming. Object is the real time entity in Object Oriented Programming (OOP). So in java everything we need to look with respective class and object. For example consider the method main (), we all know that main () method is the first line code that should be executed in the program. Here raise the question to yourself that if java is the real object oriented language means we should access the main () with object.
Like
Objectname.main();
After using this code then only main()  should execute. Hold on actually its wrong analysis.
Here we need to think before understanding it.
     1)      If we need to access the main() with object, we need to create an object for class, it says that object creation should be done first then later we need to access main() with that object.
     2)      We need to recall the basic programming concepts now, i.e. main () or static things should be access first later the remaining things should happen. Now look at the first point, by understanding the second point we can conclude that main () should not be accessible by object name.
For this reason java designed main() like this.
class ClassName
{
public static void main(String args[])
{
   //code
}
}
Note: - class contains field or methods, these fields or methods can be access by using class or object, if they are non-static we use object else they are static then we access them with class name

Why static?
    We already discussed that static things will have high or first priority in programming language, here main() is located within the class so from the point 1 & 2 we said that main() should not be accessed with object, but it should not happen for this reason main () was declared as static. Here we are going access the main () with class name at running time the program.
Example:-
C :\> java ClassName

Why public?
   Actually the main() is static so it should be access with class name, but we should not write code for accessing main () with class name in the program(Refer point 1 & 2) so we need to access it from outside of program for that reason we are using public access specifier.

Why void?
  The void means it returns nothing. Here the main() is not going to return anything so we use void in the syntax.

Why String open array?
     In java input or output is in the form of string only. The data flow in java is within the stream, so the stream supports only string format. Open array is for input values, the size is dependent upon the number of input values, for example if we provide 4 values then the size of args will be 4.
Example for command line arguments
D :\JavaBySasidhar> java classname 10  20   30  40

Simple Java program
// lang package is the default package
import java.lang.*;
class Sasi
{
 public static void main(String args[])
 {
   // printing some statement on the console
   System.out.println(“Welcome to JavaBySasidhar”);
  }
}
Save the file with class name like “Sasi.java”

Compile and Running process
D:\JavaBySasidhar> javac Sasi.java 
D:\JavaBySasidhar>java Sasi
Output :-
Welcome to JavaBySasidhar


For more example programs Click Here

No comments:

Post a Comment