Thursday, June 22, 2017

Java-Understanding access modfiers

access modifier
---------------
.An access modifier specifies accessibility of members of a program i,e

class.
variable.
method.
constructor.
interface.
enum.
inner class.

we have 4 accessmodifiers

public.
private.
default.
protected.


public
------
public is a keyword declared with

variables
methods
constructors
classes
interfaces
enum
inner class


A public member can accessed anywhere throughout the project.[It has maximum access]


syntax
------
accessmodifier datatype varname;

ex:
 public int a;


Method
------
accessmodifier returntype methodname()
{

}
ex:
public void show()
{
}

class
-----
accessmodifier class ClassName
{
}
ex:
public class A
{
}

etc


private
-------
private can be declared with

variables
methods
inner classes.

A private member can be accessed within a class itself,it is not accessible outside a class.

private is most restricted accessmodifier.

ex:
class A
{
  private int a=10;

  ....;
  ....;
}


default
-------
if we don't declare any accessmodifier in a program then the access of members of a program are said to be in default access.


ex:
class A
{
  int a=10;

  ....;
  ....;
}

Here class A, variable a are in default access.

The default access of a class in java is "default".


Any member of a program can in default accees i,e

variable
method
class
interface
enum
innerc class



A default member can be accessed anywhere in the same package but it is not allowed to access in other packages.

The scope of default members is a package itself.


protected
---------
protected keyword can be declared

variables.
methods.
inner classes.

A protected member can be accessed anywhere in the  same package and by subclasses of other package.

Arrange the following acccessmodifiers according access from highest to least

public>protected>default>private
public>default>protected>private
private>deafult>protected>public
protected>default>private>public



Note
----
we cannot declare access modifiers with local variables.

No comments: