4

If I have a class A and its subclass B.

package washington;
public class A{
       protected A(){}
}

package washington;
public class B extends A{
      public B(){super();} // it works
      public static void main (String[] args){
      A b = new A(); // it does work.
       }
}

The class A has a protected constructor, but I cannot access the constructor in B. I think it is a rule, however, I cannot find a webpage describing this situation.. All that I saw mention protected can be accessed from subclass, same-package.... e.t.c.

package california;
public class A extends washington.A{
    public static void main (String[] args){
       new washington.A(); // it does not work.
    }
}

I wonder if this is because I use IntelliJ IDEA 2017.3.4.. compiler is javac 9.0.4

kensuke1984
  • 949
  • 1
  • 11
  • 21
  • 1
    You can't assign an instance of `A` to `B`. Other than that, it works fine: https://ideone.com/VRha9m – shmosel Feb 06 '18 at 05:09
  • https://stackoverflow.com/questions/215497/in-java-difference-between-package-private-public-protected-and-private This may help clear up any confusion. – sellc Feb 06 '18 at 05:11
  • In sub class reference you can not assign to super class object – spandey Feb 06 '18 at 05:12
  • why not: `B b = new B();`? (in any package) – xerx593 Feb 06 '18 at 05:13
  • @shmosel ah it is my mistake... I changed what I really am facing... still it does not work. – kensuke1984 Feb 06 '18 at 05:15
  • You can't even reference `A` from another package because the whole class is package-protected. Please provide a [mcve]. – shmosel Feb 06 '18 at 05:17
  • @shmosel Thank you again. ..sorry over and over... it is public... i wonder if this is because IntelliJ IDEA.... (I changed a question again) – kensuke1984 Feb 06 '18 at 05:39
  • What's the relevance of `B`? – shmosel Feb 06 '18 at 05:42
  • 1
    New to me: it seems that protected methods are not generically accessable from the sub class, but only via the super keyword. I.e. you can access the constructor via `super()` and a `protected method a()` via `super.a()`, but not directly (`new california.A()` or `new california.A().a()`). So if this is a problem you should explain why you cannot make the constructor public and why you have to use the super class in your main method. – CoronA Feb 06 '18 at 05:56

2 Answers2

3

It seems that the problem is that you have your classes in different packages. Java doc says:

If the access is by a simple class instance creation expression new C(...), or a qualified class instance creation expression E.new C(...), where E is a Primary expression, or a method reference expression C :: new, where C is a ClassType, then the access is not permitted. A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) or a method reference expression only from within the package in which it is defined.

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
3
class A{
    protected A() {
        System.out.println("hello from A");
    }
}

class B extends A{
    public B() {
        super();
        System.out.println("hello from B");
    }

    public static void main (String[] args){
        A b1 = new B(); // you can either do this
        B b2 = new B(); // or this
    }
}

Try running the program and you will see the expected result printed on console.

Rishikesh Dhokare
  • 3,559
  • 23
  • 34