Showing posts with label default. Show all posts
Showing posts with label default. Show all posts

Friday, 29 January 2016

Access Specifiers in java

In java a number of access specifiers to set access levels of classes, variables, methods and constructors. They are below.

  • default
  • private
  • public
  • protected

1. default access modifier
If you don't use any modifier, it is treated as default bydefault. The default modifier is accessible only within package.

Example

//save by Sample.java 
package pack;
class Sample{
  void msg(){System.out.println("Hello");}
}  

//save by SampleOne.java  

package mypack;
import pack.*;
class SampleOne{
  public static void main(String args[]){
   Sample obj = new Sample(); // will throw Compile Time Error 
   obj.msg();// will throw Compile Time Error 
  }
}  

2. private access specifier
The private access modifier is accessible only within class.

Example:
class Sample{
private int a =10;
private void msg(){System.out.println("Hello one");}
}
public class SampleOne{
 public static void main(String args[]){
   Sample obj=new Sample();
   System.out.println(obj.a);//Compile Time Error 
   obj.msg();//Compile Time Error 
   }
}  

3.public access specifier
The public access modifier in java is accessible from everywhere inside the program. 

Example:
//save by One.java 
package example;
public class One{
public void msg(){System.out.println("Hello");}
}  
//save Two.java 
 
package myexample;
import example.*;
class Two{
  public static void main(String args[]){
   One obj = new One();
   obj.msg();
  }
}  

Output:
Hello

4.protected access specifier
Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

Example:
class One{
   protected boolean run(int a) {
      // implementation details
   }
}
//subclass
class Two extends One{
   boolean run(int a) {
      // implementation details
   }
}

Tuesday, 21 January 2014

Introducing access controls in java

how to specify access specifiers: public: private: protect

Access control:


 Encapsulation links data with the code that manipulates it. However,encapsulation provides another important attribute: access control.Java’s access specifiers are public, private, and protected. Java also defines a default access level.protected applies only when inheritance is involved. When a member of a class is modified by the public specifier, then that member can be accessed by any other code.

Saturday, 4 January 2014

Switch Statement in Java

How to use Java Switch: Case: Default: 

Using switch statement

Swich:

The switch statement is Java’s multiway branch statement.It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.It is an alternatives of else-if ladder. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an int, a String, or an enumerated value.