
10 Types of Programming Languages Every Coder should Know
Learn about different types of programming languages and how they are implemented in the real world.

Java is an object-oriented programming language that emphasizes encapsulation, one of the core principles of OOP. Access specifiers, or access modifiers, are keywords that determine the visibility of classes, methods, and variables. In Java, there are four primary access specifiers: public, private, protected, and package-private (default). The protected specifier plays a crucial role in inheritance and access control. This article delves into the protected access specifier, its characteristics, and its practical implications.
An access specifier in Java is an identifier that determines the scope of how classes, methods, and variables are implemented. It regulates how these elements can be reached from other program subsystems, essential in encapsulation. Four default access specifiers are strongly recognized, namely public, which grants access from any class in any package. Private, which permits access to the same class only; protected, which permits access within the class and in the same package of any subclass. Default or package-private only gives access within the same package as long as no specifier is declared. These specifiers regulate communication between program components and control and shield information that should not be exposed for modification or access.
| Access Specifier | Within Class | Within Package | Outside Package by Subclass only | Outside Package |
| Private | Y | N | N | N |
| Default | Y | Y | N | N |
| Protected | Y | Y | Y | N |
| Public | Y | Y | Y | Y |

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
A protected access specifier is a fundamental concept in object-oriented programming language that defines the accessibility of class members (attributes and methods). When a member is declared protected, it can be accessed within the class itself and by any subclasses inherited from it, promoting a controlled level of access. This means that while derived classes can utilize and extend the functionality of protected members, these members remain hidden from external source code, enhancing encapsulation. The protected access specifier balances provide visibility for subclassing and maintain the integrity of the class’s internal workings, ensuring that sensitive data and methods are not exposed to the outside world.
Here’s the syntax for using the protected access specifier.
class Animal{
protected String name ;
protected void makeSound(){
System.out.println("Animal Sound");
}
}
class Dog extends Animal{
public void displayInfo() {
name="Neeraj Kumar" ;
makeSound();
System.out.println("Dog's name ="+name);
}
}
class Cat extends Animal{
public void displayInfo(){
name="Whiskers" ;
makeSound();
System.out.println("Cat's name"+name) ;
}
}
class Main{
public static void main(String args[]){
Dog dog = new Dog() ;
dog.displayInfo();
Cat cat = new Cat() ;
cat.displayInfo() ;
}
}
Output
Animal Sound
Dog's name =Neeraj Kumar
Animal Sound
Cat's nameWhiskers
Let’s look at some examples of protected access specifiers in Java language.
Example 1
Program
class Animal{
protected String name ;
protected void makeSound(){
System.out.println("Animal makes the sound");
}
}
class Dog extends Animal{
void setName(String newName){
name = newName ;
}
void makeAnimalSound(){
makeSound();
}
}
class Main{
public static void main(String args[]){
Dog dog = new Dog() ;
dog.setName("Buddy");
dog.makeAnimalSound();
System.out.println("Dog name"+dog.name);
}
}
Output
Animal makes the sound
Dog nameBuddy
Example 2
Program
class Animal{
protected String name ;
protected void makeSound(){
System.out.println("Animal makes Sound") ;
}
}
class Dog extends Animal{
void setName(String newName) {
name = newName ;
}
void makeAnimalSound(){
makeSound();
}
}
class Main{
public static void main(String []args){
Dog dog = new Dog() ;
dog.setName("Buddy") ;
dog.makeAnimalSound();
System.out.println("Dog Name"+dog.name);
}
}
Output
Animal makes Sound
Dog NameBuddy
Example 3
Program
class Animal{
protected String name ;
protected void makeSound(){
System.out.println("Animal makes Sound");
}
}
class Dog{
Animal animal = new Animal() ;
void setAnimalName(String newName){
animal.name = newName ;
}
void makeAnimalSound(){
animal.makeSound();
}
}
class Main{
public static void main(String args[]){
Dog dog = new Dog() ;
dog.setAnimalName("Buddy") ;
dog.makeAnimalSound() ;
System.out.println("Animal Name = "+dog.animal.name) ;
}
}
Output
Animal makes Sound
Animal Name = Buddy

82.9%
of professionals don't believe their degree can help them get ahead at work.
Let’s discuss the characteristics of Protected Access Specifier
Let’s see some disadvantages of the Protected Access Specifier.
Also Read: Java Interview Questions and Answers
The protected access specifier plays a vital role in managing the visibility of class members. This allows variables and methods to be accessed within the same package and by subclasses, even if they reside in different packages. This access level balances encapsulation and flexibility, enabling subclasses to inherit and utilize protected members while restricting access from unrelated classes. It is particularly useful in base classes where certain properties or methods should be visible to derived classes but hidden from others. The protected access specifier fosters better source code organization and reuse in object-oriented programming. It promotes a structured approach to inheritance and encapsulation. Want to explore Java in depth? Check out Hero Vired’s Certificate Program in Full Stack Development.
Updated on October 28, 2024

Learn about different types of programming languages and how they are implemented in the real world.

Explore 10 front-end development, including key languages, its advantages and disadvantages, and how it shapes user experience in web design and functionality.