Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:30:47+00:00 2026-05-26T17:30:47+00:00

A protected class member in Java by convention, can be accessed only from within

  • 0

A protected class member in Java by convention, can be accessed only from within the class where it is declared or from it’s direct descendant inheriting class. A protected class member in the following example, however can be accessed from other class without using any inheritance concept in Java.

package classmembers;

final class Demo
{
    private int a=10;
    protected int b=20;
    public int c=30;

    public Demo(int a, int b, int c)
    {
        this.a=a;
        this.b=b;
        this.c=c;
    }

    protected void show()
    {
        System.out.println("a = "+a);
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        Demo d=new Demo(10, 20, 30);

        //System.out.print("\n"+d.a);    Wrong, since a has private access

        d.show(); //Working, even if it is protected

        System.out.print("\n"+d.b);  //Working, even if it is protected

        System.out.print("\n"+d.c);  //Working, since it is public, obviously
    }
}

In this example, the protected class members b of type int and the method show() declared within the class Demo are being accessed through the main() method even if they are explicitly declared as protected and no any inheritance concept is used.


In such a scenario, what is the difference between a protected class member and a public class member in Java. Are they same?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:30:48+00:00Added an answer on May 26, 2026 at 5:30 pm

    This is because protected members can also be accessed from classes in the same package. (And both Main and Demo are in the classmembers package.)

    In such a scenario, what is the difference between a protected class member and a public class member in Java. Are they same?

    The modifiers only differ if the classes are of different packages.

    Have a look at the table at the official tutorial: Controlling Access to Members of a Class

    Modifier        Class    Package    Subclass     World
    ------------------------------------------------------
    public            Y        Y            Y          Y  <---.
                                                              |---- difference!
    protected         Y        Y            Y          N  <---'
                              ---
    
    no modifier       Y        Y            N          N
    private           Y        N            N          N
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Protected Means, we can access this member only in a deriving class, and internal
Is it recommended to set member variables of a base class to protected, so
When I try to serialize class with protected members, I get the following errors:
Suppose I have the following declaration: class Over1 { protected: class Under1 { };
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have a template class for which I need to access a protected member
I recently discovered that a method in a derived class can only access the
In a Java program, I have multiple subclasses inheriting from a parent (which is
Many Java frameworks allow class members used for injection to be declared non-public. For
I set to member of class attibute DataMember. [DataMember] internal protected string _FirstName=; [DataMember]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.