tags:

views:

109

answers:

5

In the below code the Consumer class can access the protected method of Parent class.How is it possible since there is no relation between Parent and Consumer class.Please explain

    class Parent {

        public void method1(){
            System.out.println("PUBLIC METHOD");
        }
        private void method2(){
            System.out.println("PRIVATE METHOD");
        }
        protected void method3(){
            System.out.println("PROTECTED METHOD");
        }
        }

public class Consumer {
        public static void main(String[] args){
            Parent parentObj = new Parent();
            parentObj.method1();
            //parentObj.method2();
            parentObj.method3();
        }
        }

Thanks

+2  A: 

First of all, they're in the same package. Secondly, Consumer is an inner class of Parent.

Adam Crume
no Adam , Consumer is not inner class of Parent.
JavaUser
@JavaUser: Sorry, I misread the brace placement.
Adam Crume
@adam was my first thought too
atamanroman
@Adam .... Why cant I access the private methods even though Consumer is in same package?
JavaUser
@javauser look at my post for explanation
atamanroman
@JavaUser: Because that's what private means. Private members are only accessible inside the same class (and by implication, in nested classes). David Hall's comment had a good reference you should look at: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
Adam Crume
A: 

Adam you right! They are in the same package thats why consumer class is able to access protected method of parent class. Consumer is not an inner class of parent.

+6  A: 

Protected means: same package or by inheritance. Since your classes are both in the default package (never do that in real life), protected enables access. By the way: if you tried to test java access control, you forgot default access.

Private access on the other hand means: access from nowhere except this particular class (and non-static inner classes, which are still member of the host-class).

atamanroman
+1  A: 

Due to package protection.

http://mindprod.com/jgloss/packagescope.html

Monis Iqbal
+1  A: 

Here are the relevant excerpts from the Java Language Specification:

JLS 6.6 Access Control

The Java programming language provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class. If access is permitted, then the accessed entity is said to be accessible.

JLS 6.6.1 Determining Accessibility

  • [...]
  • A member/constructor of a reference type is accessible only if the type is accessible and the member/constructor is declared to permit access:
    • public: access is permitted.
    • protected: access is permitted only when one of the following is true:
      • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.
      • Access is correct as described in JLS 6.6.2 Details on protected Access.
        • A protected member/constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
    • private: access is permitted if and only if it occurs within the body of the top level class that encloses the declaration of the member or constructor.
    • Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.

The section in bold is the answer to the question in this scenario: Parent and Consumer belong to the same package, thus, at the very least, protected members of Parent are accessible from Consumer.

See also

polygenelubricants