This discussion is archived
1 2 Previous Next 17 Replies Latest reply: Apr 2, 2013 2:29 PM by user4547842 RSS

protected - an elementary question

user4547842 Newbie
Currently Being Moderated
I can not really understand why the line:
ProtectedConstructor o = new ProtectedConstructor(); // error
is not correct.
I am using eclipse and it suggest to change the visibility to protected but the visibility is protected:
protected ProtectedConstructor() {}
**************************************************************************************
package aaa;

public class ProtectedConstructor {
     protected ProtectedConstructor() {
     }
}
**************************************************************************************
package bbb;

import aaa.ProtectedConstructor;

class MyProtectedConstructor extends ProtectedConstructor {
     ProtectedConstructor o = new ProtectedConstructor();
}
**************************************************************************************
$javac -version
javac 1.7.0_17

$javac aaa\ProtectedConstructor.java

$javac bbb\TestConstructor.java
bbb\TestConstructor.java:6: error: ProtectedConstructor() has protected access in ProtectedConstructor
ProtectedConstructor o = new ProtectedConstructor();
^
1 error

Kind regards
Mariusz

Edited by: user4547842 on Apr 2, 2013 4:59 AM
  • 1. Re: protected - an elementary question
    gimbal2 Guru
    Currently Being Moderated
    class MyProtectedConstructor extends ProtectedConstructor {
    $javac bbb\TestConstructor.java
    That's not the same class. Either you're not posting the actual code you're using, or you're confusing which code you actually want to compile. And please:

    a) use the New to Java forum when you're new to Java
    b) use \
     tags when posting code                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 2. Re: protected - an elementary question
    PhHein Guru Moderator
    Currently Being Moderated
    Mod: moved from JP
  • 3. Re: protected - an elementary question
    gimbal2 Guru
    Currently Being Moderated
    It is certainly not the original code as it isn't even valid Java. It would be more like this:
    class MyProtectedConstructor extends ProtectedConstructor {
      public MyProtectedConstructor() {
        ProtectedConstructor o = new ProtectedConstructor(); // error
      }
    }
    and then the question rises: why do you want to create an instance of ProtectedConstructor there? The class itself IS-A ProtectedConstructor.
  • 4. Re: protected - an elementary question
    user4547842 Newbie
    Currently Being Moderated
    Thank you for quick answer. The code is correct. The classes MyProtectedConstructor is in the file TestConstructor.java. It is not forbidden, if the class is not public.
  • 5. Re: protected - an elementary question
    PhHein Guru Moderator
    Currently Being Moderated
    Anyway, protected means package private. As MyProtectedConstructor is in a different package, the constructor for ProtectedConstructor is not visible.
  • 6. Re: protected - an elementary question
    user4547842 Newbie
    Currently Being Moderated
    Why I must write:
    class MyProtectedConstructor extends ProtectedConstructor {
    public MyProtectedConstructor() {
    ProtectedConstructor o = new ProtectedConstructor(); // error
    }
    }
    and I can not write:
    class MyProtectedConstructor extends ProtectedConstructor {
    ProtectedConstructor o = new ProtectedConstructor(); // error
    }
    why do you want to create an instance of ProtectedConstructor there?
    It can be used for example in some design patterns.
  • 7. Re: protected - an elementary question
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    user4547842 wrote:
    why do you want to create an instance of ProtectedConstructor there?
    It can be used for example in some design patterns.
    Could you please tell which?

    bye
    TPD
  • 8. Re: protected - an elementary question
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    user4547842 wrote:
    Why I must write:
    class MyProtectedConstructor extends ProtectedConstructor {
    public MyProtectedConstructor() {
    ProtectedConstructor o = new ProtectedConstructor(); // error
    }
    }
    and I can not write:
    class MyProtectedConstructor extends ProtectedConstructor {
    ProtectedConstructor o = new ProtectedConstructor(); // error
    }
    Both is syntactically correct.
    The first creates a local variable in the constructor, the second creates an initialised object variable.

    Nevertheless: both is useless.

    bye
    TPD
  • 9. Re: protected - an elementary question
    user4547842 Newbie
    Currently Being Moderated
    Anyway, protected means package private. As MyProtectedConstructor is in a different package, the constructor for ProtectedConstructor is not visible.
    Definitely not, protected means that you can see this in the package and in all classes which inherit from the class.

    package aaa;
    public class ProtectedConstructor {
         protected int i;
         protected ProtectedConstructor() {
         }
    }

    package bbb;
    import aaa.ProtectedConstructor;
    class MyProtectedConstructor extends ProtectedConstructor {
         ProtectedConstructor o = new ProtectedConstructor(); //ERROR
         int j = i;     //NOT ERROR
    }

    The constructor is protected and the field i is also protected. In class MyProtectedConstructor which extends ProtectedConstructor I can use the field i bu I can not use the constructor. Why?
  • 10. Re: protected - an elementary question
    gimbal2 Guru
    Currently Being Moderated
    user4547842 wrote:
    Why I must write:
    class MyProtectedConstructor extends ProtectedConstructor {
    public MyProtectedConstructor() {
    ProtectedConstructor o = new ProtectedConstructor(); // error
    }
    }
    and I can not write:
    class MyProtectedConstructor extends ProtectedConstructor {
    ProtectedConstructor o = new ProtectedConstructor(); // error
    }
    Well you can, but the fact that you also extend from ProtectedConstructor is highly confusing. I think you just want this:
    class MyProtectedConstructor extends ProtectedConstructor {
       public MyProtectedConstructor() {
       }
    }
    Now you cannot create an instance of ProtectedConstructor, but you can create an instance of MyProtectedConstructor. In creating a MyProtectedConstructor instance the ProtectedConstructor's constructor is also invoked.

    But - its confusing and your naming of classes is not making it easier.
  • 11. Re: protected - an elementary question
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    user4547842 wrote:
    The constructor is protected and the field i is also protected. In class MyProtectedConstructor which extends ProtectedConstructor I can use the field i bu I can not use the constructor. Why?
    Have you read this?
    http://stackoverflow.com/questions/5150748/protected-constructor-and-accessibility

    bye
    TPD
  • 12. Re: protected - an elementary question
    user4547842 Newbie
    Currently Being Moderated
    Thank you. This is very useful link.
  • 13. Re: protected - an elementary question
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    user4547842 wrote:
    Thank you. This is very useful link.
    I'm still courious about the design pattern you wanted to express using this technique...

    bye
    TPD
  • 14. Re: protected - an elementary question
    user4547842 Newbie
    Currently Being Moderated
    I was thinking about the decorator, but this design pattern is just an example.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points