0

I have a class A in package p1 that has a protected constructor. I have another class B in a different package p2 that is a subclass of class A. The requirement is to access the protected constructor of class A in some method of class B without changing the access modifier of the constructor in class A.

When I am trying to do the same, I am getting an error saying that the constructor is not visible.

package p1;
public class A 
{
    protected A()
    {
        System.out.println("Hello");
    }
}

package p2;
import p1.A;
public class B extends p1.A
{
    public static void main(String args[])
    {
        new A();  // Error is on this line
    }
}

Note that I can access the constructor of class A within the constructor of class B using super() but the requirement is to access it within some function of class B without changing the access modifier of the constructor of class A.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Akash Nigam
  • 300
  • 2
  • 5
  • 11
  • I'm unclear what your problem is. The constructor of B **is** a "function of class B", and you've already said that you can call A's constructor using `super()` there... – azurefrog Oct 13 '15 at 20:31
  • The problem is that I dont want to access the constructor of class A within the constructor of class B. I want to access it in some function of class B (I know that constructor is a special function of a class). Hope you have understood – Akash Nigam Oct 13 '15 at 20:33
  • Class B is Class A since it extends from it. If you just create new ClassB() and the only line in ClassB constructor is super() then you are basically just initializing class A – Naveed Oct 13 '15 at 20:37
  • Naveed, super() cant be used in a function of class B. The requirement is to access the constructor of class A from some function of class B (not constructor). Hope u understood the requirement better now !! – Akash Nigam Oct 13 '15 at 20:43
  • @AkashNigam The rules just don't allow it. The only way to use a protected constructor of the super class in a different package is to do `super()` in a constructor. You could always just write `new B()` instead. – Paul Boddington Oct 13 '15 at 20:45
  • @AkashNigam That is against Java rules `protected` means package visibility. You cannot access a protected constructor outside of package unless its called from the constructor of subclass – Naveed Oct 13 '15 at 20:50
  • @PaulBoddington - Thanks for the reply !! This is strange. Had class B been in the same package as class A, we could have easily accessed the constructor of class A using new A() in some function of class B (refer : http://i.imgur.com/th2egEL.png) but this is not allowed in a different package. Really strange !! – Akash Nigam Oct 13 '15 at 20:52
  • Don't ever forget the following when dealing with `protected` members: [_A protected member or 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._](http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2) – Sotirios Delimanolis Oct 13 '15 at 20:55

1 Answers1

0

You are providing the constructor for class A but you are making it protected so you cannot instantiate class A. You can only create an instance of class B. You have not hidden the constructor of class B and it is given the default constructor.

jiveturkey
  • 2,484
  • 1
  • 23
  • 41
  • That is not correct. It is possible to instantiate a class that has a protected constructor. Refer : http://i.imgur.com/th2egEL.png – Akash Nigam Oct 13 '15 at 20:39