Access Modifiers in java
Modifiers are keywords that are added to change meaning of a definition. In Java Programming Language, modifiers are catagorized into two types.
Access control modifier
and
Non Access control modifier.
Access control modifier
Java Programming Language has four access modifier to control access levels for classes, variables, methods and constructors. These are
Default: Default has scope only inside the same package
Public: Public has scope it is visible everywhere
Protected: Protected has scope within the package and all sub classes
Private: Private has scope only within the classes
Non Access control modifier.
Non access modifiers do not change the accessibility of variables and methods, but they do provide them special properties. Java provides a number of non-access modifiers to achieve many other functionality.Static: The static modifier for creating class methods and variables
Final: The final modifier for finalizing the implementations of classes, methods, and variables.
Final modifier is used to declare a field as final i.e. it prevents its content from being modified. Final field must be initialized when it is declared.
For example:
class Cloth
{
final int MAX_PRICE = 999; //final variable
final int MIN_PRICE = 699;
final void display() //final method
{
System.out.println("Maxprice is" + MAX_PRICE );
System.out.println("Minprice is" + MIN_PRICE);
}
}
A class can also be declared as final. A class declared as final cannot be inherited. String class in java.lang package is a example of final class. Method declared as final can be inherited but you cannot override/redefine it.
Abstract: The abstract modifier for creating abstract classes and methods.
Abstract Class:
An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended.
A class cannot be both abstract and final. If a class contains abstract methods then the class should be declared abstract. Otherwise a compile error will be thrown.
An abstract class may contain both abstract methods as well normal methods.
For example:
abstract class Jet{
private double price;
private String model;
private String year;
public abstract void goFast(); // an abstract method
public abstract void changeColor();
}
Abstract Methods:
An abstract method is a method declared with out any implementation. The methods body(implementation) is provided by the subclass. Abstract methods can never be final or strict.
Any class that extends an abstract class must implement all the abstract methods of the super class unless the subclass is also an abstract class.
If a class contains one or more abstract methods then the class must be declared abstract. An abstract class does not need to contain abstract methods.
The abstract method ends with a semicolon. Example: public abstract sample();
For Example:
public abstract class SuperClass{
abstract void m(); //abstract method
}
class SubClass extends SuperClass{
// implements the abstract method
void m(){
...
}
}
Synchronized and volatile: The synchronized and volatile modifiers, which are used for threads. When a method is synchronized it can be accessed by only one thread at a time. Volatile modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program. Volatile variables are used in case of multithreading program.
synchronized Modifier
The synchronized key word used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers.
For example:
public synchronized void showDetails(){
.......
}
transient Modifier
An instance variable is marked transient to indicate the JVM to skip the particular variable when serializing the object containing it.
This modifier is included in the statement that creates the variable, preceding the class or data type of the variable.
For example:
public transient int limit = 55; // will not persist
public int b; // will persist
volatile Modifier
The volatile is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.
Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.
For example:
public class MyRunnable implements Runnable
{
private volatile boolean active;
public void run()
{
active = true;
while (active)
{
// Code goes here
}
}
public void stop()
{
active = false;
}
}
Understanding Java Programming Language access modifiers
Java access modifiers with method overriding
class A{protected void msg(){System.out.println("Hello java");}
}
public class Simple extends A{
void msg(){System.out.println("Hello java");}//C.T.Error
public static void main(String args[]){
Simple obj=new Simple();
obj.msg();
}
}