Home > DeveloperSection > Forums > In Java, what's the difference between public, default, protected, and private?
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    May-06-2015 12:08 AM

 Java Java 
Ratings:


 1 Reply(s)
 520  View(s)
Rate this:
Are there clear rules on when to use each of these when making classes and interfaces and dealing with inheritance?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    May-06-2015 12:55 AM

This Java tutorial may be of some use to you.

Modifier    | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  y    |    y    |    y     |   y
————————————+———————+—————————+——————————+———————
protected   |  y    |    y    |    y     |   n
————————————+———————+—————————+——————————+———————
no modifier |  y    |    y    |    n     |   n    **also known as package-private**
————————————+———————+—————————+——————————+———————
private     |  y    |    n    |    n     |   n

y: accessible
n: not accessible

Don't want to miss updates? Please click the below button!

Follow MindStick