Rabu, 28 Mei 2014

Difference between private, protected, public and package modifier or keyword in Java




private vs public vs protected vs package in Java


Java has four access modifier namely private, protected and public. package
level access is default access level provided by Java  if no access modifier is specified. These
access modifier is used to restrict accessibility of a class,
method or variable
on which it applies. We will start from private
access modifier
which is most restrictive access modifier and then go towards
public which is least restrictive access modifier, along the way we will see
some best
practices
while using access modifier in Java and some examples of using
private and protected keywords.






private
keyword in Java



private vs public vs package vs protected access in Java

private keyword or modifier in java can
be applied to member field, method or nested
class in Java
. you can not use
private modifier
on top level class.
private variables, methods and class are
only accessible on the class on which they are declared.
private is highest
form of Encapsulation
Java API provides and should be used as much as possible. Its best
coding practice
in Java to declare variable private by default.
private method can
only be called from the class where it has declared. As per Rules
of method overriding in Java
,
private method can
not be overridden as well.
private keyword can also be applied
to constructor and if you make constructor private you prevent it from being
sub-classed. popular example of making constructor private  is Singleton
class in Java
which provides getInstance() method to get object instead of
creating new object using constructor in Java. here are some differences
between
private and protected, public and package
level access





package
or default access level in Java


there is no access modifier called package instead package is a
keyword which is used to declare package
in Java
, package is a directory on which a class
in Java
belongs. Package or default access level is second highest
restrictive access modifier after
private and any
variable, method or class declared as
package-private is only
accessible on the package it belongs. good thing about default modifier is
that, top level class can also be package-private if there is no class level
access modifier.





protected
keyword in Java


Difference between private and protected keyword is
that protected method, variable or nested
class
not only accessible inside class, inside package but also outside of
package on subclass. if you declare a variable
protected means any
one can use it if they extend your class. top level class can not be make
protected as well.





public
keyword in Java


public is the least restrictive access
modifier in Java programming language and its bad practice to declare field,
method or class by default public because once you make it public its very
difficult to make any change on internal structure of class as it affect all
clients using it. Making class or instance
variable
public also violated principle of Encapsulation
which is not good at all and affects maintenance badly. instead of making
variable
public you should make it private and
provided public getter and setter.
public modifier
can also be applied to top level class. In Java name of file must be same with
public class declared in the file.





That's all difference between private, protected, package and public access
modifier. As you have seen difference between private and public lies on how
accessible a particular field, method or class would have. public means you can
access it anywhere while private means you can only access it inside its own
class.





Just to note all private, protected or public modifier
are not applicable to local
variables in Java
. local variable can only be final
in java
.





Other Java fundamentals tutorials from Javarevisited





























Source:http://javarevisited.blogspot.com/2012/10/difference-between-private-protected-public-package-access-java.html

Tidak ada komentar:

Posting Komentar