0

So I know this is confusing but why does this work? If I have

public class A implements Parcelable{
  private B propB;
}

public class B extends A implements Parcelable{
  private int stuff;
}

so if I ever created a

A objectA = new A(); 

wouldn't there be an infinite loop of this: objectA.propB.propB.propB?

the main issue is that I want to pass an instance of class B in android as a bundle, but this is not working, no error, just data doesn't get passed through. All the properties and data gets passed through except for that one property that seems to be a circular dependency..

is there a reason why this is good practice? or is it just not a good idea?

reidisaki
  • 1,525
  • 16
  • 29
  • 2
    To clarify if you changed it to `private B propB = new B()` you would get a Stack Overflow...this was in response to another comment that disappeared. – George Mulligan Jan 22 '16 at 03:46
  • why not just pass in object B as bundle? You will definately get stack overflow when u try to create new instance of class B. – kopikaokao Jan 22 '16 at 04:13
  • So this is good practice to have a property that will be a subclass of that superclass it's a property of? – reidisaki Jan 22 '16 at 16:28

2 Answers2

0

there is no error occurs in any case, because the B object only a private field of A, and is not initialization. but, if you create a new B instance, you only create a new object A().

if you want to use
objectA.propB.propB.propB.......
you can get the NullPointException.

xun
  • 1
  • 1
0

The reason why data wasn't being passed through is because of parcels. I wasn't calling super(in) on the sub class B. so although there was no error there wasn't any data passing through either.

reidisaki
  • 1,525
  • 16
  • 29