java protected method accessibility

Posted by JavaUser on Stack Overflow See other posts from Stack Overflow or by JavaUser
Published on 2010-06-17T04:58:13Z Indexed on 2010/06/17 5:03 UTC
Read the original article Hit count: 238

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about oop