
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between protected and default access specifiers in Java?
The Protected access specifier is visible within the same package and also visible in the subclass whereas the Default is a package level access specifier and it can be visible in the same package.
Protected Access Specifier
- Protected will acts as public within the same package and acts as private outside the package.
- Protected will also act as public outside the package only with respect to subclass objects.
- Protected fields or methods cannot be used for classes and Interfaces.
- The Fields, methods, and constructors declared as protected in a superclass can be accessed only by subclasses in other packages.
- The classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.
Example
public class ProtectedTest { // variables that are protected protected int age = 30; protected String name = "Adithya"; /** * This method is declared as protected. */ protected String getInfo() { return name +" is "+ age +" years old."; } public static void main(String[] args) { System.out.println(new ProtectedTest().getInfo()); } }
Output
Adithya is 30 years old.
Default Access Specifier
- Any member of a class mentioned without any access specifier then it is considered that as Default.
- The Default will act as public within the same package and acts as private outside the package.
- The Default members of any class can be available to anything within the same package and can not be available outside the package under any condition.
- The Default restricts the access only to package level, even after extending the class having default data members we cannot able to access.
Example
public class DefaultTest { // variables that have no access modifier int age = 25; String name = "Jai"; /** * This method is declared with default aacees specifier */ String getInfo() { return name +" is "+ age +" years old."; } public static void main(String[] args) { System.out.println(new DefaultTest().getInfo()); } }
Output
Jai is 25 years old.
- Related Articles
- What are the differences between public, protected and private access specifiers in C#?
- What are private, public, default and protected access Java modifiers?
- What are the differences between access modifiers and non-access modifiers in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- What are access specifiers in C#.NET?
- What are the different access specifiers in C#.NET?
- protected access modifier in Java
- What are the differences between Microsoft Excel and Microsoft Access?
- What is the scope of protected access modifier in Java?
- WiFi Protected Access (WPA) and WiFi Protected Access 2 (WPA2)
- Why Protected access modifier used in Java?
- What is Default access level in Java?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between Java classes and Java objects?

Advertisements