-By Nazmin Shaikh
Access Modifiers
Access modifiers ( or access specifiers ) are keywords
in object-oriented languages that set the accessibility
of classes, methods, and other members. Access
modifiers are a specific part of programming language
syntax used to facilitate the encapsulation of
components.
~Types
1.default
2.private
3.protected
4.public
Default private protected public
Same Class Yes Yes Yes Yes
Same Package
sub class
Yes No Yes Yes
Same Package
non-sub class
Yes No Yes Yes
Different
Package
sub class
No No Yes Yes
Different
Package non-
sub class
NO No No Yes
Default
 When no access modifier is specified for a class,
method or data member – It is said to be having
the default access modifier by default.
 The data members, class or methods which are not
declared using any access modifiers i.e. having
default access modifiers are accessible only within
the same package.
package p1;
class Geek
|{
void display ( )
| {
system . out . Print1n (“Hello World ! ’’) ;
. }
.}
package p2;
Import p1. *;
class GeekNew
|{
public static void main (string args [ ] )
{
Geeks obj = new Greek ()_ ;
obj .display ( ) ;
}
.}
Private
 The private access modifier is specified using the
keyword private.
 The methods or data members declared as private
are accessible only within the class in which they
are declared.
 Any other class of same package will not be
able to access these members.
 Classes or interface can not be declared as private.
package p1;
Class A
|{
private void displays ( )
| {
system.out.Print1n ( “GeeksforGeeks”) ;
. }
.}
Class B
|{
public static void main (string args[ ] )
| {
A obj = new A ( ) ;
obj . display ( );
. }
}
Protected
 The protected access modifier is specified using
the keyword protected.
 The methods or data members declared as
protected are accessible within same package or
sub classes in different package.
 Classes cannot be declared protected.
Package p1 ;
Public class A
{
protected void display ( )
{
System. Out . print1n ( “GeeksforGeeks” ) ;
}
}
package p2 ;
Import p1.*;
Class B extends A
{
public static void main ( string args [ ] )
{
B obj = new B ( ) ;
obj .display ( ) ;
}
}
Public
 The public access modifier is specified using the
keyword public.
 The public access modifier has the widest scope
among all other access modifiers.
 Classes, methods or data members which are
declared as public are accessible from every
where in the program. There is no restriction on
the scope of a public data members.
package p1;
public class A
{
public void display( )
{
system. out . Print1n( “GeeksforGeeks” ) ;
}
}
package p2;
Import p1 .*;
class B
{
public static void main(string args [ ])
{
A obj = new A;
obj. display ( ) ;
}
}

Visibility Modifiers for Access Control.pptx

  • 1.
  • 2.
    Access Modifiers Access modifiers( or access specifiers ) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components. ~Types 1.default 2.private 3.protected 4.public
  • 3.
    Default private protectedpublic Same Class Yes Yes Yes Yes Same Package sub class Yes No Yes Yes Same Package non-sub class Yes No Yes Yes Different Package sub class No No Yes Yes Different Package non- sub class NO No No Yes
  • 4.
    Default  When noaccess modifier is specified for a class, method or data member – It is said to be having the default access modifier by default.  The data members, class or methods which are not declared using any access modifiers i.e. having default access modifiers are accessible only within the same package.
  • 5.
    package p1; class Geek |{ voiddisplay ( ) | { system . out . Print1n (“Hello World ! ’’) ; . } .} package p2; Import p1. *; class GeekNew |{ public static void main (string args [ ] ) { Geeks obj = new Greek ()_ ; obj .display ( ) ; } .}
  • 6.
    Private  The privateaccess modifier is specified using the keyword private.  The methods or data members declared as private are accessible only within the class in which they are declared.  Any other class of same package will not be able to access these members.  Classes or interface can not be declared as private.
  • 7.
    package p1; Class A |{ privatevoid displays ( ) | { system.out.Print1n ( “GeeksforGeeks”) ; . } .} Class B |{ public static void main (string args[ ] ) | { A obj = new A ( ) ; obj . display ( ); . } }
  • 8.
    Protected  The protectedaccess modifier is specified using the keyword protected.  The methods or data members declared as protected are accessible within same package or sub classes in different package.  Classes cannot be declared protected.
  • 9.
    Package p1 ; Publicclass A { protected void display ( ) { System. Out . print1n ( “GeeksforGeeks” ) ; } } package p2 ; Import p1.*; Class B extends A { public static void main ( string args [ ] ) { B obj = new B ( ) ; obj .display ( ) ; } }
  • 10.
    Public  The publicaccess modifier is specified using the keyword public.  The public access modifier has the widest scope among all other access modifiers.  Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members.
  • 11.
    package p1; public classA { public void display( ) { system. out . Print1n( “GeeksforGeeks” ) ; } } package p2; Import p1 .*; class B { public static void main(string args [ ]) { A obj = new A; obj. display ( ) ; } }