public
protected
default (no modifier)
private
public:
protected:
default (no modifier):
private:
| Access Modifier | Same Class | Same Package | Subclass (Other Package) | Other Package |
|---|---|---|---|---|
| public | ✅ | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ✅ | ❌ |
| default | ✅ | ✅ | ❌ | ❌ |
| private | ✅ | ❌ | ❌ | ❌ |
public class Car
{
private String model;
protected int speed;
public void startEngine()
{
System.out.println("Engine started");
}
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.