I am trying to understand extending inner classes in Java. I have read around but nothing I found quite answers my question. So here goes...
I have...
public class Pie{
protected Slice[] slices;
// Pie constructor
public Pie(int n){
sliceGenerator(n)
}
private void sliceGenerator(int n){
slices = new Slice[n];
final float sweepAngle = 360.0f/(float)n;
float startAngle = 0;
for (int i=0;i<n;i++){
slices[i] = new Slice(startAngle);
startAngle += sweepAngle;
}
}
@Override
public String toString(){
for (Slice s:slices){
s.toString();
}
}
// Inner class...
public class Slice{
public Slice(float startAngle){
//set some private fields based on startAngle and generic pie
}
@Override
public String toString(){
return **string based on private fields**
}
}
}
Then I extend this...
public class ApplePie extends Pie{
protected Slice[] slices;
// Apple Pie constructor
public ApplePie(int n){
super(n);
}
// Inner class...
public class Slice extends Pie.Slice{
public Slice(float startAngle){
super(startAngle);
//set some **additional** private fields based on startAngle **specific to apple pie** appleness or something
}
@Override
public String toString(){
return **string based on apple pie specific private fields**
}
}
}
Now, when I make an Apple pie and call its toString method, like so...
ApplePie ap = new ApplePie(8);
System.out.println(ap.toString());
I do not get information about the apple pie slices, but information about the pie slices. It ignores my toString
override, or more likely ignores my apple pie Slice
. How can I arrange it such that apple pie slices refer to ApplePie
?
Any help much appreciated! Sorry for pie references - it is the actual class I am working with...
I've changed your code to meet your requirements.
Your super class
Pie
is about to create a new instance ofSlice
, but the child class ApplePie's Slice does not override theSlice
method of its super class'.I added the functions below to enable the child class to create its own
Slice
.Pie.java:
ApplePie.java:
Test:
The code will print
22222222
The answer lies within your program. When you instantiate Slice class, it gives call to the super class and invokes sliceGenerator. This method internally creates instances of Pie.Slice and not ApplePie.Slice. To get around this, make sliceGenerator method protected and override it in Apple.Slice class. Create the instances of Apple.Slice and it should work.
In your superclass, you are creating and storing
Pie.Slice
objects:These are the same objects being used by
Pie.toString
(whichApplePie
doesn't override by the way).Extending
Pie
withApplePie
and extendingPie.Slice
withApplePie.Slice
doesn't change this. Thenew Slice(startAngle)
in the above code does not magically switch to instantiating something different.Aside from that, your
Pie.toString()
isn't returning anything - it shouldn't even compile:I'm guessing you want to return a String representing all the slices. This would be a quick solution for example:
(
Arrays.toString
is just a utility method to get a String representing of an array.)