Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»What’s the Difference Between Public, Default, Protected and Private Members in Java

    What’s the Difference Between Public, Default, Protected and Private Members in Java

    By RahulSeptember 11, 20222 Mins Read

    Access modifiers are special keywords that define the accessibility or scope of a member. We can specify how other code can interact with specific methods or variables within or outside classes and packages. Like many programming concepts, access modifiers are simple in practice but can seem confusing at first. Once you understand them, they can help make your code more readable and maintainable.

    Advertisement

    Understand the Difference Between Members

    Java provides 4 types of access modifiers with different scopes and visibility. Any member without any specifier is dealt with as “Default” or “package-private”.

    1. Private: A private member is accessible only in the same class
    2. Default (package-private): A member without any access modifiers is known as default or package private. Which is accessible in all classes of the same package
    3. Protected: A protected member is accessing all classes of the same package and subclasses of the other packages.
    4. Public: A public member is accessible all classes of all packages

    See the below graphical representation, which will help you better outstanding about the scope of all members in different cases.

    What is Access Modifiers in Java
    Access Modifiers and Their Scope in Java

    Hope this tutorial helped to under the difference between private, package-private (default), protected, and public members. Please put your valuable suggestions in the comment box.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    System.out.println() Method in Java: A Beginner’s Guide

    sleep Command in Linux with Examples

    20 Basic Linux Commands for the Beginners (Recommended)

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.