Wednesday, 18 May 2016

Modifires in java

A modifier is special type of keyword which is used for authentication purpose. There are different types of modifiers in java and every modifiers provide different accessibility. Java have two types of modifiers:
1)  Access modifiers:
Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors.
a)   Default:
Visible to the package, the default. No modifiers are needed.
b)   Public:
Visible to the world. Everyone can access the public modifier.
c)   Private:
Visible to class only. If you have many classes in package then you cannot able to access from other class when you declared any method, constructor, and block as private
d)   Protected:
Visible to package only. If you have many classes in package then you can able to access from other class when you declared any method, constructor, and block as protected.


2)  Non-Access modifiers:
a)   Static:
·         Static keyword in java is used for memory management mainly. We can apply java static keyword with variable, method and class.
·         Static method can invoke without any object.
·         Static Block also can invoke without object.
·         Static method can access static data member and can change the value of it.
b)   Abstract:
Abstract class be used if you want to share code among several closely related classes, the class that has common code can be declared as abstract class.
·         An abstract method is a method that is declared without an implementation like this: abstract void salary(double basicpay)


c)   Final:
·         Final keyword is used for method, class and variable.
·         Whenever we declared a variable as final then we don’t able to change it. It is behave like a constant. Eg:  final float PI=3.14;
·         Whenever we declared a method as final then we don’t able to override.
·         Whenever we declared a class as final then we don’t able to extend.

0 comments:

Post a Comment