Lesson 3 of 4 Object Oriented Programming in Java
1 of 11
Lesson 3: Access Modifiers
Author: Kasun Ranga Wijeweera
Email:
[email protected]
Date: 2016 December 21
Access modifiers control the access to methods and attributes. There
are four access modifiers in java: default, public, private, and
protected.
Examples
private int x;
public static void main (String args[])
{
}
Default access modifier: If we do not specify any access modifier then
that attribute or method will have default access modifier. Such
attribute or method is available only to any other class in the same
package.
Public access modifier: The public access modifier allows an attribute
or a method available to any other class.
Private access modifier: An attribute or a method with private access
modifier can only be accessed within their own class.
Protected access modifier: An attribute or a method with protected
access modifier can only be accessed by its subclasses and by classes
of the same package.