I understand the concept of a top-level class that extends an inner class:
package pkg1;
public class A {
public class B {
}
}
package pkg2;
import pk1.A;
public class C extends A.B {
public C() {
new A().super();
}
}
But I cannot figure out any real example that illustrates this. That is, why should we use such implementation?
That is, why should we use such implementation?
You shouldn't. If C has to create its own enclosing instance, then it is no longer semantically "inner".
This feature is more useful when you are passing the enclosing instance in, which behaves the same as an inner class:
class C extends A.B {
C(A enclosing) {
enclosing.super(); // note: invokes a constructor
} // of the B superclass
}
(Except we can't use a class instance creation expression like someA.new C(). We have to use new C(someA).)
But if you find yourself having to use this, it probably means you've programmed yourself in to a corner. B should probably be a top-level class or static with the "enclosing instance" explicitly passed in.
Related
I have two base classes, and a subclass extending one base class with an inner class extending the other class.
Base Classes:
public class B1 {}
public class B2 {}
Subclass:
public class A extends B1{
class B extends B2{}
}
Method:
execute(B1 b) {
..do something
}
Call:
A a = new A();
A.B inner = a.new B();
execute(inner);
This is failing. Does the inner class not extend the outer class's super class? Is there a way in the inner classes constructor to call the outer class's constructor?
Say something like this (failing now..)
B() {
super();
A()/outer();
}
Modification with static method:
public class A extends B1{
static B getInner() {
return new B();
}
static class B extends B2{}
}
Call to static method:
A.B inner = A.getInner();
No, and Java doesn't have multiple inheritance. So if an inner class did extend the outer class (or the outer class' super class) then class B extends B2{} would be illegal.
No, an inner class cannot extend the outer class' superclass.
You clarified in a comment that you want to circumvent the multiple inheritance rule. Although Java classes do not directly support multiple inheritance, Java 8 introduced default methods for interfaces, which may satisfy the desire for multiple inheritance in certain situations.
(I keep re-reading that question title and thinking about how ridiculous it must look, but I assure you that is the best description of the problem, and I have an actual application where this is the best structure. I swear I'm not crazy.)
Consider the following. Each block is a separate file:
package myPackage;
public class A {
public int i;
public A(int i) {
this.i = i;
}
public class B {
}
}
package myPackage;
import myPackage.A.B;
public class Main {
public static void main(String[] args) {
class C extends B {
public C(A enclosingInstance) {
enclosingInstance.super();
}
public void show() {
System.out.println(A.this.i);
}
}
A myA = new A(2);
C myC = new C(myA);
myC.show();
}
}
Note that the enclosingInstance business is to solve a problem involving intermediate constructor invocations. See "Why can't outer classes extend inner classes?".
I would expect the output to be "2". But instead, I have a compile error on System.out.println(A.this.i);:
No enclosing instance of the type A is accessible in scope
I think the programmatic concept I'm trying to solve is sound: Create a new type of B inside main to give to A that uses things from A that types of B can access.
So what am I doing wrong, or why isn't this possible in java?
EDIT/UPDATE: Note that the same error appears when the code in main is moved to a non-static method. That is to say, I tried moving everything inside of static void main to a new, non-static method of class Main called go(). Then I changed static void main to the single line new Main().go();. The error is in the same spot. So it doesn't seem to be an issue of class C being defined in a static context.
You want A.this to refer to the enclosing instance of the B instance. But why should it? That's not what the syntax means. A.this would mean the enclosing A instance of the C instance, and this does not make sense because C is not an inner class of A.
To make this clearer, here is an example where C is an inner class of A.
public class A {
public int i;
public A(int i) {
this.i = i;
}
public class B {
void foo() {
System.out.println(A.this.i);
}
}
public class C extends B {
C(A a) {
a.super();
}
void bar() {
System.out.println(A.this.i);
}
}
public static void main(String[] args) {
A a1 = new A(1);
A a2 = new A(2);
C c = a1.new C(a2);
c.foo();
c.bar();
}
}
Here C extends B, and both C and B are inner classes of A. Therefore any C has an enclosing A instance, and it also has an enclosing A instance when considered as a B, and these enclosing instances are different (as proved by the fact that foo and bar print different numbers).
So, A.this could not possibly mean what you want it to mean, because it already means something else. I guess the reason why the language designers didn't come up with other syntax to mean the enclosing instance of a super class, is because such syntax would be very complicated, with little pay-off (simple workarounds already exist).
This is absurd code that you should never write for production.
It is, in part, explained in the documentation for Explicit Constructor Invocations
Qualified superclass constructor invocations begin with a Primary
expression or an ExpressionName. They allow a subclass constructor to
explicitly specify the newly created object's immediately enclosing
instance with respect to the direct superclass (§8.1.3). This may be
necessary when the superclass is an inner class.
All this to say that C is a local class (which is an inner class, which is kind of nonsense because if you declare it in a static method there is no enclosing instance) that is a subclass of B but not a nested class of A. As such, there is no enclosing instance. An instance of C does not have an enclosing instance. (Though it would if you declared it in an instance method, but that would be an instance of Main.)
The newly created object's immediately enclosing instance (from JLS) is specified indirectly through a constructor parameter.
You'd have to store it yourself
private A enclosingInstance;
public C(A enclosingInstance) throws CloneNotSupportedException {
enclosingInstance.super();
this.enclosingInstance = enclosingInstance;
}
and since A#i is public, you can access it normally
public void show() {
System.out.println(enclosingInstance.i);
}
With the provided information, I would do this :
public class B {
protected A getOuterInstance() {
return A.this;
}
}
and just let C inherit and use this method. I know you dislike this method but this is the simplest answer I can see. With more information, I would probably propose a design which would try not involving any inner class as this is not a normal use case for inner classes.
Considering the following code:
public abstract class AbstractA
{
public static final class B
{
protected B(){}
}
}
// a class (in another package) that inherits from AbstractA
public class C extends AbstractA
{
B[] arrayOfB=new B[10];
for(byte i=0; i<=arrayOfB.length; i++)
{
arrayOfB[i]=new B();
}
}
In class C I can define arrayOfB because class B is static & public but I cant instanciate an object of this.
Eclipse says: The constructor A.B() is not visible
If class C was in the same package as the class A, I could instantiate it.
How can I keep the constructor B() protected and still create an object of this knowing that the class C inherits from A?
It is nothing to do with inner classes. Your B constructor is protected it means only subclasses could access it, but you define the class as final, it doesn't make sense. Maybe you could add a factory method to the AbstractA which creates B instances, outer class has access to its inner classes, even for private methods.
public abstract class AbstractA
{
protected B newB() {return new B();}
public static final class B
{
private B(){}
}
}
// a class (in another package) that inherits from AbstractA
public class C extends AbstractA
{
B[] arrayOfB=new B[10];
for(byte i=0; i<=arrayOfB.length; i++)
{
arrayOfB[i]=this.newB();
}
}
Public static classes that are nested are the same as public classes in a separate file. So you cannot see B's constructor, because you are not B's descendant, nor are you in the same package.
If you want to extends a class outside its package it must have a constructor that is public or protected because in Java every constructor must call a constructor from its superclass.
(The implicit super() call will fail.)
The protected specifier allows access by all subclasses of the class in question, whatever package they reside in, as well as to other code in the same package.
In your case, the class C resides in different package and hence you are not allowed to instantiate A.B() due to above reason.
Please remember, protected specifier permits only inheritance in different package. You cannot access them directly..
As a solution to your problem, move the class C in the same package as that of AbstractA to make B accessible.
I have 6 classes as shown in figure below.
Now, class A has an object of class B as a private variable defined. Also class A methods calls many methods from class B, for example B.method1().
Now, class A_Base1 is which is derived from class A, needs to call methods from the derived class B_Base1; for example B1.method2(). And also methods of class A_Base2 needs to call methods from class B_Base2; for example B2.method3().
Now in class A I define the variable as -
private B bObject
Now in method of A_Base1, I cannot cannot call the methods like bObject.method2() since its a base class object.
I need suggestions on -
Is it possible to call derived class object methods using base class object?
Or do I need to re-design this scenario in some other good way?
Using inheritance like this imo only makes sense if the Bx.methodX() do something that means she same to the different Ax. And in that case, you should name them that way:
public class B {
public void doWhatAMeans() {
method1();
}
public class B1 extends B {
#Override
public void doWhatAMeans() {
method2();
}
public class B2 extends B {
#Override
public void doWhatAMeans() {
method3();
}
and then you only need A to call doWhatAMeans() and the A1 and A2 only need to be injected the appopriate instances of Bx.
On the other hand, if doWhatAMeans does not make sense because the methodX do different things that mean different things to Ax, then you need to rethink your object model, probably the parallel structures A,A1,A2 and B,B1,B2 are wrong then.
you could always cast. suppose your class A provides this method:
protected B getBInstance() {
return bObject;
}
then in A_Base1 you could do something like:
((B_Base1)getBInstance()).method2();
this, however, is a VERY bad design. if your A_Base1 class needs an instance of B_Base1 it should be handed such an instance directly at construction time:
public class A_Base1 extends A {
private B_Base1 b1Object;
public A_Base1(B_Base1 instance) {
super(B_Base1); //works as a B for parent
this.b1Ovject = instance;
}
}
and then you can use that
since A is a parent of A_Base1 (I'm assuming extended) you can make the function call that Accesses B public (or protected) and then A_Base1 or A_Base2 can use the same function A does to call into B.
I am using a hierarchy of inner classes to represent some data in an application and I have run into an error message that I simply do not understand. My code can be boiled down to the following minimal example:
public class A {
public class B extends A {}
public class C extends B {}
}
Javac (and my IDE of course) fails to compile the code with the following error message:
A.java:3: cannot reference this before supertype constructor has been called
public class C extends B {}
^
1 error
I didn't write this anywhere. There is no more code than provided above, so I assume javac has generated something related to the inner class.
I have found another way to represent my data, so I am simply interested in a good explanation of why it doesn't compile.
You need an outer class instance to create an inner class instance i.e something like new Outer().new Inner();
To extend the inner class (Parent inner class) with another inner class (child inner class), you cannot call the constructor of 'parent inner class' because the instance of 'outer class' is not there.
Try like this,
public class A{
public class B extends A {
B() { }
}
public class C extends B {
C() {
new A().super();
}
}
public static void main(String args[]) {
}
}
Similar question : Odd situation for “cannot reference this before supertype constructor has been called”
The other poster is correct, but how to fix? Simply make your class static:
public class A {
public static class B extends A {}
public static class C extends B {}
}
Note that if your inner classes refer to fields of the outer class, you can't make them static, otherwise you can (and should - doing so reduces dependencies).
Your code compiles under Java 7.
The following workaround compiles under Java 6.
public class C extends B
{
public C()
{
A.this.super();
}
}
#saugok's link to the previous question quoted Joshua's explanation. Basically he argued that since C is subclass of A, C inherits A's members as C's members. Therefore B is also C's member. (For example a class literal C.B.class is valid.) Therefore he argues that C.this is the enclosing instance for B's super(), therefore C(){super();} is actually C(){C.this.super();}. Since C.this cannot be evaluated before super constructor, thus the error.
However this doesn't seem to be warranted by the language spec. See #8.1.3. Since B is not immediately lexically enclosed by C, B is not a direct inner class of C, there is no reason to say that B's direct enclosing instance must be an instance of C.
We need to pass B() an instance of A. It is true that C.this is an instance of A ( try this code: new C().new B().new C().new B();) therefore it could be a candidate. There is also another candidate, A.this. A.this is available and ready to use (it's passed in as the hidden parameter to C()).
According to javap, javac 7 compiles the code into
class B
private A this$0;
B( A a )
this$0 = a;
super(); // A()
class C extends B
private A this$0;
C( A a )
this$0 = a;
super( a ); // B(A)