0% found this document useful (0 votes)
3 views5 pages

Java Unit 2.3

The document explains access modifiers in Java, including public, protected, default, and private, detailing their visibility and usage. It provides examples for each modifier to illustrate how they control access within classes and packages. Additionally, it discusses methods to access private members from outside the class, such as using getter and setter methods, inner classes, and local classes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Unit 2.3

The document explains access modifiers in Java, including public, protected, default, and private, detailing their visibility and usage. It provides examples for each modifier to illustrate how they control access within classes and packages. Additionally, it discusses methods to access private members from outside the class, such as using getter and setter methods, inner classes, and local classes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Leela Soft Core Java (J2SE) Madhusudhan

• Visibility: The member is accessible within the same package and by subclasses (even
if they are in different packages).

[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
• Usage: Use protected for members that should be accessible within the package and
to subclasses but not to unrelated classes.

Example:
//[Link] (File Name)
package sample1;

public class Employee {


protected void display() {
[Link]("Welcome");
}
}

//[Link] (File Name)


package sample2;

import sample1;

class Test extends Employee{


public static void main(String args[]) {
Test t = new Test();
[Link]();
}
}

Output: Hello

3. Default (Package-Level)
• If we don't use any modifier, it is treated as 'default' by default.
• Visibility: The member is accessible only within the same package. No keyword is used
for this level of access.
• Usage: Use default access for members that should only be accessible within the same
package.

Example:
//[Link] (File Name)
package sample1;

public class Employee {


void display() {
[Link]("Welcome");
}
}

[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
//[Link] (File Name)
package sample2;

import sample1;

[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

class Test {
public static void main(String args[]) {
Employee emp = new Employee(); //Compilation fine
[Link](); //Compile Time Error
}
}

4. Private (private)
• Visibility: The member is accessible only within the class in which it is declared.
• Usage: Use private for members that should be completely hidden from other classes.

Example:
//[Link] (File Name)
public class Employee {

private int empID; //private variable


private void display() {
[Link]("Welcome");
}
}

public class Simple {


public static void main(String args[]) {
Employee emp = new Employee();
[Link]([Link]);// Compile Time Error
[Link]();// Compile Time Error
}
}

Summary of Access Levels


Modifier Within Class Within Package outside package by outside package
subclass only
public Yes Yes Yes Yes
protected Yes Yes Yes No
(default) Yes Yes No No
private Yes No No No

Accessing Private Members of Class:


In Java, private members (fields, methods, constructors) are designed to be accessible only
within the class in which they are defined. However, there are several ways to access private
members from outside the class, though some methods should be used with caution. Here
are the primary methods:
• Accessing Through the Same Class
[Link]@[Link] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
• Using Getter and Setter Methods
• Inner Classes
• Local Classes

[Link]@[Link] Cell: 78 42 66 47 66

You might also like