Question about calling constructors of inner classes across differentpackages

D

Dural

Here's my scenario: I have an interface, a class with an inner class
that implements the interface, and a second class that extends the
first class (and the inner class as well, since I've made it
protected.) The interface, base class, and derived class are all in
separate packages.

Here are my interfaces and classes: Interface I, base class A, inner
class AI (extends interface I), derived class B:

-------------------------------------------------------------------------------------
package testbed.subpacka;

public interface I {
void f();
}

--------------------------------------------------------------------------------------
package testbed.subpackb;

import testbed.subpacka.*;

public class A {

protected class AI implements I {
public AI() { // Remove public and B (below) won't compile
System.out.println("AI constructed");
}
public void f() {
System.out.println("AI.f()");
}
}
}
-----------------------------------------------------------------------------------
package innerclasses;

import testbed.subpacka.*;
import testbed.subpackb.*;

public class B extends A { // Inner class AI and function f()
inherited from A

public I g() {
return new AI();
}
public static void main(String[] args) {
B b= new B();
I t = b.g();
t.f();
}
}

------------------------------------------------------------------------------
Again, note the different packages, as that's what leads to the issue
at hand:

My problem lies with the B.g() method. Unless I make a AI's
constructor public, B can't return a new AI() in the g() method. I
don't want to make AI's constructor public if I can avoid it.

It seems odd that a class can inherit a protected inner class from a
base class, yet be unable to create a new instance of the inner class
unless the constructor is public, merely because the base and derived
classes are in different packages. Is there a simple way around this
problem?

Thanks for any feedback -
 
D

Daniel Pitts

Dural said:
Here's my scenario: I have an interface, a class with an inner class
that implements the interface, and a second class that extends the
first class (and the inner class as well, since I've made it
protected.) The interface, base class, and derived class are all in
separate packages.

Here are my interfaces and classes: Interface I, base class A, inner
class AI (extends interface I), derived class B:

-------------------------------------------------------------------------------------
package testbed.subpacka;

public interface I {
void f();
}

--------------------------------------------------------------------------------------
package testbed.subpackb;

import testbed.subpacka.*;

public class A {

protected class AI implements I {
public AI() { // Remove public and B (below) won't compile
System.out.println("AI constructed");
}
public void f() {
System.out.println("AI.f()");
}
}
}
-----------------------------------------------------------------------------------
package innerclasses;

import testbed.subpacka.*;
import testbed.subpackb.*;

public class B extends A { // Inner class AI and function f()
inherited from A

public I g() {
return new AI();
}
public static void main(String[] args) {
B b= new B();
I t = b.g();
t.f();
}
}

------------------------------------------------------------------------------
Again, note the different packages, as that's what leads to the issue
at hand:

My problem lies with the B.g() method. Unless I make a AI's
constructor public, B can't return a new AI() in the g() method. I
don't want to make AI's constructor public if I can avoid it.

It seems odd that a class can inherit a protected inner class from a
base class, yet be unable to create a new instance of the inner class
unless the constructor is public, merely because the base and derived
classes are in different packages. Is there a simple way around this
problem?

Thanks for any feedback -
The easiest way is to make A provide a protected method that is a
factory for AI.
 
D

Daniele Futtorovic

Here's my scenario: I have an interface, a class with an inner class
that implements the interface, and a second class that extends the
first class (and the inner class as well, since I've made it
protected.) The interface, base class, and derived class are all in
separate packages.

Here are my interfaces and classes: Interface I, base class A, inner
class AI (extends interface I), derived class B:

-------------------------------------------------------------------------------------
package testbed.subpacka;

public interface I { void f(); }

--------------------------------------------------------------------------------------
package testbed.subpackb;

import testbed.subpacka.*;

public class A {

protected class AI implements I { public AI() { // Remove public
and B (below) won't compile System.out.println("AI constructed"); }
public void f() { System.out.println("AI.f()"); } } }
-----------------------------------------------------------------------------------
package innerclasses;

import testbed.subpacka.*; import testbed.subpackb.*;

public class B extends A { // Inner class AI and function f()
inherited from A

public I g() { return new AI(); } public static void main(String[]
args) { B b= new B(); I t = b.g(); t.f(); } }

------------------------------------------------------------------------------
Again, note the different packages, as that's what leads to the
issue at hand:

My problem lies with the B.g() method. Unless I make a AI's
constructor public, B can't return a new AI() in the g() method. I
don't want to make AI's constructor public if I can avoid it.

It seems odd that a class can inherit a protected inner class from a
base class, yet be unable to create a new instance of the inner class
unless the constructor is public, merely because the base and
derived classes are in different packages. Is there a simple way
around this problem?

Thanks for any feedback -

It doesn't really matter for the c'tor to be public. The c'tor will only
be visible to code to whom the class is visible too.
If you want finer control over the accessibility, using a factory method
like Daniel suggested would be the way to go.

DF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top