0% found this document useful (0 votes)
8 views14 pages

Java Access Specifiers Explained

The document explains the behavior of different access specifiers (default, private, protected, and public) in Java, detailing their accessibility within the same class, same package, and different packages. It includes code examples demonstrating successful and unsuccessful access attempts based on the access level of class members. The document illustrates how access specifiers affect inheritance and visibility in various scenarios.

Uploaded by

abhimahapatra17
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)
8 views14 pages

Java Access Specifiers Explained

The document explains the behavior of different access specifiers (default, private, protected, and public) in Java, detailing their accessibility within the same class, same package, and different packages. It includes code examples demonstrating successful and unsuccessful access attempts based on the access level of class members. The document illustrates how access specifiers affect inheritance and visibility in various scenarios.

Uploaded by

abhimahapatra17
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

default access specifier

Same class {YES}


class A
{
int a=100;
void display()
{
[Link]("DEFAULT ACCESS SPECIFIER CAN BE ACCESS
WITHIN SAME CLASS value of a="+a);

}
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}
Compile-javac def_same_class.java
Run-java A
Output-DEFAULT ACCESS SPECIFIER CAN BE ACCESS WITHIN SAME CLASS value of a=100
Same package sub-class{YES}
package p;
class A
{
int a=100;
void display()
{
[Link]("DEFAULT ACCESS SPECIFIER CAN BE ACCESS
WITHIN SAME package by the sub class value of a="+a);

}
}
class B extends A
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}
Compile-javac -d . samepackage_subclass.java
Run-java p.B
Output-DEFAULT ACCESS SPECIFIER CAN BE ACCESS WITHIN SAME package by the sub class
value of a=100

Same package non-sub-class{YES}


package p;
class A
{
int a=100;
void display()
{
[Link]("DEFAULT ACCESS SPECIFIER CAN BE ACCESS
WITHIN SAME package by the non_sub_class value of a="+a);

}
}
class B
{
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}

Compile- javac -d . samepackage_bynon_subclass.java


Run-java p.B
Output- DEFAULT ACCESS SPECIFIER CAN BE ACCESS WITHIN SAME package by the
non_sub_class value of a=100

Different package sub-class{NO}


package p;
class A
{
int a=200;
void display()
{
[Link]("DEFAULT ACCESS SPECIFIER CAN BE ACCESS
WITHIN SAME CLASS value of a="+a);

}
Compile- javac -d . def_same_class.java
Run- java p.A
Output- Error: Main method not found in class p.A, please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend [Link]
package p1;
import p.*;
class B extends A
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}
Compile- javac -d . different_package_sub_class.java
Output- different_package_sub_class.java:3: error: A is not public in p; cannot
be accessed from outside package
class B extends A
^
different_package_sub_class.java:8: error: cannot find symbol
[Link]();
^
symbol: method display()
location: variable obj of type B
2 errors

Different package non-subclass{NO}


package p;
class A
{
int a=200;
void display()
{
[Link]("DEFAULT ACCESS SPECIFIER CAN BE ACCESS
WITHIN SAME CLASS value of a="+a);

}
Compile- javac -d . def_same_class.java
Run- java p.A
Output- Error: Main method not found in class p.A, please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend [Link]

package p1;
import p.*;
class B
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}

Compile- javac -d . different_package_non_subclass.java


Output- different_package_non_subclass.java:8: error: cannot find symbol
[Link]();
^
symbol: method display()
location: variable obj of type B
1 error

private access specifier


Same class {YES}
class pvt
{
private int a=100;
void display()
{
[Link]("THIS IS A PRIVATE ACCESS SPECIFIER CLASS PVT
CAN BE ACCESSED WITHIN CLASS ONLY THE VALUE OF a="+a);
}
public static void main(String args[])
{
pvt obj=new pvt();
[Link]();
}
}
Compile- javac pvt_same_class.java
Run- java pvt
Output- THIS IS A PRIVATE ACCESS SPECIFIER CLASS PVT CAN BE ACCESSED
WITHIN CLASS ONLY THE VALUE OF a=100
Same package by the sub-class{NO}
package pvt1;
class pvt
{
private int a=100;
private void display()
{
[Link]("THIS IS A PRIVATE ACCESS SPECIFIER CLASS PVT
CAN BE ACCESSED WITHIN CLASS ONLY THE VALUE OF a="+a);
}
}

class pvtsub extends pvt


{
public static void main(String args[])
{
pvt obj=new pvt();
[Link]();
}
}
Compile- javac -d . pvt_same_package_sub_class.java
Output- pvt_same_package_sub_class.java:15: error: display() has private
access in pvt
[Link]();
^
1 error

protected access specifier


Same class {YES}
class A
{
protected int a=100;
protected void display()
{
[Link]("This is a protected class can be access within
same class value of a="+a);
}
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}
Compile- javac protected_same_class.java
Run- java A
Output- This is a protected class can be access within same class value of a=100

Same package sub-class{YES}


package protect;
class A
{
protected int a=100;
protected void display()
{
[Link]("This is a protected class can be access within
same package by the sub class value of a="+a);
}
}
class B extends A
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}
Compile- javac -d . protected_same_package_sub_class.java
Run- java protect.B
Output- This is a protected class can be access within same package by the sub
class value of a=100

Same package non-sub-class{YES}


package protect;
class A
{
protected int a=100;
protected void display()
{
[Link]("This is a protected class can be access within
same package by the non sub class value of a="+a);
}
}
class B
{
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}
Compile- javac -d . protected_same_package_non_sub_class.java
Run- java protect.B
Output- This is a protected class can be access within same package by the non
sub class value of a=100

Different package sub-class{YES}


package protect;
public class protected_sameclass
{
protected int a=100;
protected void display()
{
[Link]("This is a protected class can be access within
different package by the sub class value of a="+a);
}
}
Compile- javac -d . protected_sameclass.java
Run- java protect.protected_sameclass
Output- Error: Main method not found in class protect.protected_sameclass,
please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend [Link]

package protect1;
import protect.*;
class B extends protected_sameclass
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}
Compile- javac -d . protected_different_package_subclass.java
Run- java protect1.B
Output- This is a protected class can be access within different package by the
sub class value of a=100

Different package non-sub-class{NO}

public access specifier


Same class {YES}
class A
{
public int a=100;
public void display()
{
[Link]("This is a public access specifier can be access
within same class value of a="+a);
}
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}
Compile- javac public_same_class.java
Run- java A
Output- This is a public access specifier can be access within same class value of
a=100

Same package sub-class{YES}


package pub;
class A
{
public int a=100;
public void display()
{
[Link]("This is a public access specifier can be access
within same package by the sub class value of a="+a);
}
}

class B extends A
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}
Compile- javac -d . public_same_package_subclass.java
Run- java pub.B
Output- This is a public access specifier can be access within same package by
the sub class value of a=100
Same package non-sub-class{YES}
package pub;
class A
{
public int a=100;
public void display()
{
[Link]("This is a public access specifier can be access
within same package by the non sub class value of a="+a);
}
}
class B
{
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}
Compile- javac -d . public_same_package_non_subclass.java
Run- java pub.B
Output- This is a public access specifier can be access within same package by
the non sub class value of a=100

Different package sub-class{YES}


package pub;
public class public_same_class
{
public int a=100;
public void display()
{
[Link]("This is a public access specifier can be access
within diffrent package by the sub class value of a="+a);
}
}
Compile- javac -d . public_same_class.java
Run- java pub.public_same_class
Output- Error: Main method not found in class pub.public_same_class, please
define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend [Link]

package pub1;
import pub.*;
class B extends public_same_class
{
public static void main(String args[])
{
B obj=new B();
[Link]();
}
}
Compile- javac -d . public_diffrent_package_sub_class.java
Run- java pub1.B
Output- This is a public access specifier can be access within diffrent package by
the sub class value of a=100

Different package non-subclass{YES}


package pub;
public class public_same_class
{
public int a=100;
public void display()
{
[Link]("This is a public access specifier can be access
within diffrent package by the non sub class value of a="+a);
}
}
Compile- javac -d . public_same_class.java
Run- java pub.public_same_class
Output- Error: Main method not found in class pub.public_same_class, please
define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend [Link]

package pub1;
import pub.*;
class B
{
public static void main(String args[])
{
public_same_class obj=new public_same_class();
[Link]();
}
}
Compile- javac -d . public_diffrent_package_non_sub_class.java
Run- java pub1.B
Output- This is a public access specifier can be access within diffrent package by
the non sub class value of a=100

Common questions

Powered by AI

Both 'default' and 'protected' access specifiers allow access within the same package by both subclasses and non-subclasses. In the context of the same package, non-subclass access is granted for both, meaning other classes within the same package can access the members. However, the difference lies in their behavior with subclasses in different packages: 'default' restricts access strictly to the package, whereas 'protected' allows access by subclass outside the package. This is demonstrated in the case where both specifiers worked similarly for non-sub-classes within the same package .

Choosing an access specifier profoundly affects software design by dictating the level of data encapsulation and the scope of inheritance. 'Private' access ensures strict encapsulation by confining member access within the class, preventing external manipulation or inheritance. 'Protected' access expands to subclasses and package members, supporting inheritance and fostering controlled extensibility across packages. 'Public' extends this visibility worldwide, facilitating open interfacing and integration. These choices influence class flexibility, security, and architecture by determining which components can interact or be extended. For example, 'protected' provides subclass inheritance across packages, pivotal for frameworks necessitating extensible base designs .

'Default' access specifiers restrict access strictly to the same package, meaning different package subclasses can't access them, which is highlighted by the absence of access in the 'different package sub-class{NO}' example . In contrast,'protected' access specifiers allow subclasses to inherit and access even when they reside in different packages, as seen where protected members are accessed by a subclass in another package . The design intention behind this is that 'protected' is meant to provide visibility to subclassing hierarchy beyond package boundaries, whereas 'default' assumes no such relationship or visibility is needed outside a package.

Compilation errors due to access specifiers often arise from attempts to access members not visible in the current class context. For instance, 'default' access specifiers cause errors if a class from a different package attempts to access them since it limits access to the package level. Similarly, 'private' access restricts access to the class itself, causing errors when accessed elsewhere. Such errors reinforce encapsulation principles and ensure that proper member visibility is adhered to during the compilation. These restrictions ensure controlled access to class members, as shown by access attempts across different packages resulting in compilation errors .

Variables and methods with no explicit access specifier, often referred to as default access, can be accessed within the same class, within the same package by both subclasses and non-subclasses, but not from a different package. For instance, in the case of a class within the package 'p', a default access specifier allows access within the same class and same package for both sub-classes and non-sub-classes, but it doesn't allow access from a sub-class in a different package or a non-sub-class in a different package, as evidenced by compile errors shown when trying to access the class or methods from another package .

The 'private' access specifier restricts access strictly to the same class, meaning private members cannot be accessed by any other class, regardless if they are in the same package or extend the same class. This is illustrated in the document where a class with a private property cannot be accessed by a subclass within the same package, resulting in a compile error .

The 'private' access specifier imposes strict visibility restrictions, completely preventing subclass inheritance of the private members. This means any attempt to access or override the private members from a subclass will result in a compile-time error. This stringent restriction underlies encapsulation, ensuring that private data is not manipulated outside the class they reside in. For example, an error occurs when a subclass in the same package tries to access a private method, as shown in an attempt to compile 'pvt_same_package_sub_class.java' .

The 'protected' access specifier allows access within the same package and by subclasses in different packages. This implies that a subclass in another package can access the protected members of its superclass. However, if a class is neither in the same package nor a subclass, it cannot access the protected members. In the provided sources, it is shown that a class with protected members can be extended and accessed by a subclass in a different package, but cannot be accessed by a non-subclass in a different package .

In a subclass, 'protected' methods can be accessed and overridden, providing greater flexibility in subclass behavior modification, whereas 'private' methods are inaccessible, highlighting encapsulation. This distinction emphasizes how 'protected' engenders subclass cooperation across packages, while 'private' guards against inter-class data misuse. The document illustrates an instance where a private method leads to compile errors in the subclass context, whereas the protected method is accessible and functional in a subclass in another package .

Using the 'public' access specifier ensures that a class's methods and variables are accessible from any other class within any package. This means there are no restrictions imposed by package boundary in accessing public members, as they are globally available wherever the class is visible (i.e., imported). In the examples provided, public methods can be accessed both within the same and different packages, by both subclasses and non-subclasses .

You might also like