0% found this document useful (0 votes)
14 views2 pages

Access Modifiers

Methods and instance variables are collectively known as members. There are four access modifiers: public, private, default, and protected. Public members can be accessed by any class, private members can only be accessed within their own class, protected members can be accessed by subclasses even if in a different package, and default members can only be accessed by classes in the same package. Local variables cannot have access modifiers applied and can only be declared final.

Uploaded by

pravicheers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Access Modifiers

Methods and instance variables are collectively known as members. There are four access modifiers: public, private, default, and protected. Public members can be accessed by any class, private members can only be accessed within their own class, protected members can be accessed by subclasses even if in a different package, and default members can only be accessed by classes in the same package. Local variables cannot have access modifiers applied and can only be declared final.

Uploaded by

pravicheers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Methods and instance variables are collectively known as members.

Access Modifiers:

1) public 2)private 3)default 4)protected

A class can use only two modifiers (public and default). whereas a member can use all
the four modifiers.

What does it mean code in one class have access to a member of another class?

1) Whether method code in one class can access a member of another class
2) Whether a subclass can inherit a member of its superclass

Public Members
When a method or variable member is declared public, it means all other classes,
regardless of the package they
belong to, can access the member (assuming the class itself is visible).
Private Members
Members marked private can't be accessed by code in any class other than the
class in which the private member was declared.

What about a subclass that tries to inherit a private member of its superclass?
When a member is declared private, a
subclass can't inherit it.

Can a private method be overridden by a subclass? That's an interesting question,


but the answer is technically no. Since the subclass, as
we've seen, cannot inherit a private method, it therefore cannot override the
method—overriding depends on inheritance.

Protected and Default Members


The protected and default access control levels are almost identical, but with one
critical difference.
A default member may be accessed only if the class accessing the member belongs
to the same package.
Members marked protected can be accessed (through inheritance) by a subclass
even if the subclass is in a different package.
The subclass can see the protected member only through inheritance.

If the protected keyword is used to define a member, any subclass of the class
declaring the member can access it through inheritance. It doesn't matter if the superclass
and subclass are in different packages

Access modifiers cannot be applied to local variables. Class will not compile.
Only modifier that can be applied will be final.
Final Arguments:

public Record getRecord(int fileNumber, int recordNumber) {}


Method arguments are essentially the same as local variables. Arguments can be
declared final.
public Record getRecord(int fileNumber, final int recordNumber) {}
LocalVariables variables (Nonlocal) methods
final public protected private
public protected private static
final
final static transient volatile abstract synchronized strictfp
native

Transient Variables
If you mark an instance variable as transient, you're telling the JVM to skip (ignore)
this variable when you attempt to
serialize the object containing it.

Volatile Variables
The volatile modifier tells the JVM that a thread accessing the variable must always
reconcile its own private copy of the variable with the master copy in memory.

You might also like