---
title: "Protected vs Public in terms of Inheritance in Java"  
description: "Protected vs Public in terms of Inheritance in Java"  
author: "Revati S Misra"  
published: 2023-08-18  
updated: 2023-08-19  
canonical: https://www.mindstick.com/forum/159587/protected-vs-public-in-terms-of-inheritance-in-java  
category: "java"  
tags: ["java", "inheritance", "access modifiers"]  
reading_time: 2 minutes  

---

# Protected vs Public in terms of Inheritance in Java

Protected vs [Public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) in [terms](https://answers.mindstick.com/qa/31698/what-is-the-real-value-of-us-dollars-in-terms-of-indian-rupee) of [Inheritance](https://www.mindstick.com/articles/13095/inheritance-and-its-types) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)

## Replies

### Reply by Aryan Kumar

In Java, the access modifiers `public` and `protected` are used to control the visibility of members of a class, such as variables, methods, and constructors.

- **Public** members are accessible to any class, whether it is in the same package or in a different package.
- **Protected** members are accessible to any class in the same package and to subclasses of the class in any package.

In terms of inheritance, the main difference between `public` and `protected` is that `protected` members are only accessible to subclasses, even if the subclass is in a different package. This means that `protected` members can be used to implement an inheritance hierarchy without exposing the implementation details to other classes.

For example, let's say we have a class called `Animal` with a `protected` method called `getSound()`. This method returns the sound that the animal makes. We can then create a subclass of `Animal` called `Dog`. The `Dog` class can override the `getSound()` method to return the sound that a dog makes. However, other classes outside of the `Animal` and `Dog` packages cannot access the `getSound()` method.

Here is a table summarizing the visibility of `public` and `protected` members in Java:

| Access modifier | Visibility |
| --- | --- |
| Public | Any class, whether it is in the same package or in a different package. |
| Protected | Any class in the same package and subclasses of the class in any package. |
| Private | Only the class in which it is declared. |

When choosing between `public` and `protected`, you should consider the following factors:

- Do you want the member to be accessible to other classes? If yes, then use `public`.
- Do you want the member to be accessible to subclasses, even if they are in different packages? If yes, then use `protected`.
- Do you want to encapsulate the implementation details of the member? If yes, then use `private`.


---

Original Source: https://www.mindstick.com/forum/159587/protected-vs-public-in-terms-of-inheritance-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
