Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Protected members

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Protected members

    package p1;
    class A
    {
    protected void show()
    {
    Syatem.out.println("Hello");
    }
    }
    This is file B.java

    class B extends p1.A
    {
    protected void show()
    {
    A ob=new A();
    ob.show();//This shows that show in A is protected cant be acceded in B[why, while we have inherited A into B]

    System.out.println("Hello");
    }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Protected members

    Hello.
    What exactly is your question?

    Syed.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Protected members

    This is file A.java
    package p1;
    class A
    {
    protected void show()
    {
    Syatem.out.println("Hello");
    }
    }

    This is file B.java

    class B extends p1.A
    {
    protected void show()
    {
    A ob=new A();
    B ob1=new B();
    ob.show();//Cant Call
    ob1.show();Running properly
    System.out.println("Hello");
    }
    }

    why the protected member of super class can't be called in sub class by the object of super class but u can call the super class protected member can be called by sub class object.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Protected members

    hello,
    protected fields and methods are accessed through inheritance, they are not meant to be accessed by the DOT operator. try to change the ob.show() to show() only without calling it by the reference variable ob.
    thanks

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Protected members

    @Alaa that is not the correct solution to this problem. un-qualifying the object operation on results in this.show() being executed. It does not execute show() for the object ob. Use of the dot operator is perfectly compatible with inheritance and polymorphism. It is the protected access rules which are causing the compiler error.

    Ex.:

    package p;
     
    public class A
    {
        protected void show()
        {
            System.out.println("A.show");
        }
    }

    // B is not in package p
    public class B extends p.A
    {
        protected void show()
        {
            System.out.println("B.show");
        }
     
        void doit()
        {
            A a = new A();
            show(); // doesn't call A.show(), this will call this.show(). The wrong message is displayed
        }
     
        public static void main(String[] args)
        {
            A a1 = new A();
            a1.show(); // compiler error, explained below
            A a2 = new B();
            a2.show(); // compiler error, same reason as a1
            B b = new B();
            b.show(); // compiles fine, explained below
        }
    }

    The reason it fails is described in Section 6.6.2.1 of the Java Language Specifications.

    {my note: assume S is not in the same package as C. Otherwise these rules don't apply}

    Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

    In addition, if Id denotes an instance field or instance method, then:

    If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

    If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
    These rules specify that in class B, it can only access protected members of A if the expression has a type B or a subclass of B.

    In your code, the type of ob is not a B, it is type A. Thus it is not allowed to access the protected member A.show(). However, if the type is B or a subclass of B, then it is allowed access. ob1 is type B, thus this compiles just fine.

    Additionally, calling this.show() (or the short version show()) will compile fine because this is of type B.

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Protected members

    Hello.
    Can you try super.show()?

    Syed.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Protected members

    @syedbhai

    That is perfectly legal because it invokes the super method show with type B (this is of type B). However, the object called with is still this, not a newly created ob object.

    There is no way to access/expose a protected member in a super class C from a sub-class S (assuming S is not in the same package as C) unless the object we're operating on is guaranteed to be an S or a subclass of S.

    The best solution is to take advantage of the package rule for protected access and move the class B into the same package as A, or give more details on what the OP wants to do. There might be some intrinsic interface design issues which could be solved differently and avoid the problem to begin with.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    divcret (August 13th, 2013)

  9. #8
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Protected members

    Hi, this is an example of Default Access Specifier for class in Java. If class in a package is not having any access specifier, its considered as default. With default access, class cannot be access outside from package where its written. So it becomes private for anything outside its package.

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Protected members

    @divcret protected is a superset of default. Access is allowed from subclasses even if they aren't in the same package. There's just a caveat as I noted above that you have to operate on the subclass (or a child of the subclass), not the base class.

Similar Threads

  1. What's the importance of declaring a variable as PROTECTED?
    By realsoundkid in forum Java Theory & Questions
    Replies: 8
    Last Post: November 19th, 2012, 05:41 PM
  2. Why cannot the class be protected?
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: September 20th, 2012, 04:42 PM
  3. protected Access Specifier
    By spark in forum Object Oriented Programming
    Replies: 14
    Last Post: August 5th, 2012, 03:13 PM
  4. Using Protected Methods, in a Java Library
    By WACman in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 05:42 PM
  5. Replies: 2
    Last Post: June 13th, 2009, 01:44 AM