IN JAVA, WHAT'S THE DIFFERENCE BETWEEN PUBLIC, DEFAULT, PROTECTED, AND PRIVATE?

Royce Roy

Total Post:149

Points:1043
Posted by  Royce Roy
Java 
 918  View(s)
Ratings:
Rate this:
Are there clear rules on when to use each of these when making classes and interfaces and dealing with inheritance?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: In Java, what's the difference between public, default, protected, and private?

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!