0% found this document useful (0 votes)
330 views17 pages

WEEK 11 - Access Modifiers in Java

Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. There are four main access modifiers: public (accessible everywhere), protected (accessible in subclasses), default (accessible in the same package), and private (only accessible in the class itself). Private members can only be accessed within their declaring class using getter and setter methods.

Uploaded by

Super Rome
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
330 views17 pages

WEEK 11 - Access Modifiers in Java

Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. There are four main access modifiers: public (accessible everywhere), protected (accessible in subclasses), default (accessible in the same package), and private (only accessible in the class itself). Private members can only be accessed within their declaring class using getter and setter methods.

Uploaded by

Super Rome
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 17

Access

Modifiers in Java

WEEK 11
 In Java, a modifier has a reserved keyword that is
included in the definition of class, method, and
variables. A modifier adds some meanings to
these definitions. Modifiers are also
called specifiers.

 These modifiers are classified into two categories.


Some of the modifiers are called access modifiers
and some are called non-access modifiers.

 Access modifiers are reserved keywords that


provide a different level of access to classes,
methods, fields, etc. Reserved keywords for access
modifiers are public, protected, and private.
Access Modifiers in Java

Access modifiers are further classified into the


following categories:

 Default Access Modifier


 Public Access Modifier
 Protected Access Modifier
 Private Access Modifier
Default Access Modifier
 Default Access Modifier does not require to
use any reserved keyword. Any method or
variable with default access modifier can be
accessed from any class in a package. It
behaves almost similar as public access modifier
but there is a difference between them. In an
interface, variables are public static final and
methods are public by default.
Example 1: Define
Default Access
Modifiers

package defaultPackage;
class Logger {
void message(){
System.out.println(“This is a message”);
}
}

Here, Logger class has the default access modifier.


And this logger class is visible to the classes that
belong to the defaultPackage package. If you
import Logger class in different package and try
to instantiate it, you’ll get compilation error.
Default Access
//Addition.java Modifiers
package abcpackage;
public class Addition {
// Since we didn't mention any access modifier here, it would be considered as default.
int addTwoNumbers(int a, int b){
return a+b;
}
}
OUTPUT:
//Test.java Exception in thread "main"
package xyzpackage; java.lang.Error: Unresolved
/* We are importing the abcpackage
* but still we will get error because the compilation problem: The
method addTwoNumbers(int,
* class we are trying to use has default access
* modifier.
*/
int) from the type Addition is
import abcpackage.*; not visible at
public class Test {
public static void main(String args[]){
xyzpackage.Test.main(Test.jav
Addition obj = new Addition(); a:12)
/* It will throw error because we are trying to access
* the default method in another package
*/
obj.addTwoNumbers(10, 21);
}
}
Public Access Modifier
 In a Public Access Modifier, the keyword public is
used before variable, method, class, or
constructor. A public method or variable can be
accessed in any class, even the class that
belongs to a different package. If a class
belongs to a different package, we can import it
using inheritance.
Public Access
// Logger.java Modifiers
public class Logger {
public int debugLevel = 1;
public void debug(String logLine){
System.out.println("Debug: "+logLine);Here, in LoggerImp class, you
}
were able to instantiate the
public void info(String logLine){
Logger class because it’s access
System.out.println("Info: "+logLine);
}
modifier is public. The variables
} and methods inside the
LoggerImp class are also public.
// LoggerImp.java Hence, you are able to use it
public class LoggerImp { directly in your LoggerImp class.

public static void main( String[] args ) {


Logger logger = new Logger();
logger.debug("debug with level " + logger.debugLevel);
logger.debugLevel = 5;
logger.info("info with level " + logger.debugLevel);
}
}
OUTPUT:
Debug: debug with level
1
Info: info with level 5
Public Access
Modifiers
//Addition.java
package abcpackage;

public class Addition {

public int addTwoNumbers(int a, int b){


return a+b; Here, the method
} addTwoNumbers() has public
} modifier and class Test is able to
//Test.java access this method without even
package xyzpackage; extending the Addition class.
import abcpackage.*;
This is because public modifier
class Test{
has visibility everywhere.
public static void main(String args[]){
Addition obj = new Addition();
System.out.println(obj.addTwoNumbers(100, 1));
}
}
OUTPUT:

101
Protected Access Modifier

 In a Protected Access Modifier, the keyword


protected is used before a variable, method,
and constructor. No class can have protected
access modifier. Any variable, method, or
constructor with a protected access modifier is
only accessible within child classes or
subclasses of superclasses in which it is
declared.
Protected Access
// Logger.java Modifier
package package1;

public class Logger {


protected void debug(String logLine){
System.out.println("Debug line: "+logLine);
} As you can see, the Logger.java
} and Main.java are in different
package. The debug() method
// Main.java
in Logger class in protected and
package package2;
import package1.Logger;
this method can be accessed only
inside the package1. However, it
public class Main extends Logger { is accessed in the Main class as
well. This is all possible because
public static void main(String [] args){ Main class inherits the Logger
Main logger = new Main(); class.

// invokes debug() from Logger class


logger.debug("hello from main");
}
} OUTPUT:
Debug line: hello from main
Protected Access
Modifier
//Addition.java
package abcpackage;
public class Addition {

protected int addTwoNumbers(int a, int b){


return a+b;
In this
example the class Test
} which is present in another
} package is able to call the
addTwoNumbers() method,
which is declared protected. This
//Test.java is because the Test class
package xyzpackage; extends class Addition and the
import abcpackage.*; protected modifier allows the
access of protected members in
class Test extends Addition{
subclasses (in any packages).
public static void main(String args[]){
Test obj = new Test();
System.out.println(obj.addTwoNumbers(11, 22));
}
}
OUTPUT:
33
Private Access Modifier
 In a Private Access Modifier, the keyword private
is used before variable, method, and constructor.
A class and interface cannot be private.
A class can have private access modifier only if it
is an inner class, i.e. a class is a member of
another class.
 Private provides the most restricted level of
access
Private Access Modifier
 To access any variable or field outside of the class
in which it is declared, setter and getter
methods are used.
 Getter methods are used to access the
variable outside class.
 Setter methods are used to set values for these
private fields in a class where these are declared.
Private Access
Modifiers

public class Data {


private String name;
public String getName() {
return this.name;
} Here, name is a private variable
public void setName(String name) { and it is visible only inside the
this.format = name;
Data class. But, it can be
}
accessed in another class, Main
}
public class Main {
,by the help of public getter and
Public static void main(String[] main){ setter methods.
Data d = new Data();
d.setName(“Evangeline”);
System.out.println(d.getName());
}
}
OUTPUT:
Evangeline
Private Access
Modifiers

class ABC{
This example throws compilation
private double num = 100; error because we are trying to
private int square(int a){ access the private data member
return a*a; and method of class ABC in the
} class Example. The private data
} member and method are only
accessible within the class.
public class Example{
public static void main(String args[]){
ABC obj = new ABC();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
OUTPUT:
Compile - time error
THANK
YOU!

You might also like