Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:43:12+00:00 2026-06-17T20:43:12+00:00

I am trying to understand extending inner classes in Java. I have read around

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:43:14+00:00Added an answer on June 17, 2026 at 8:43 pm

    I’ve changed your code to meet your requirements.

    Your super class Pie is about to create a new instance of Slice, but the child class ApplePie’s Slice does not override the Slice method of its super class’.

    I added the functions below to enable the child class to create its own Slice.

    protected void newSliceArray(int n) {
        slices = new Slice[n];
    }
    
    protected Slice newSlice(float startAngle) {
        return new Slice(startAngle);
    }
    

    Pie.java:

    public class Pie {
      private int a = 1;
      protected Slice[] slices;
    
      // Pie constructor
      public Pie(int n) {
        sliceGenerator(n);
      }
    
      private void sliceGenerator(int n) {
        newSliceArray(n);
        final float sweepAngle = 360.0f / n;
        float startAngle = 0;
        for (int i = 0; i < n; i++) {
          slices[i] = newSlice(startAngle);
          startAngle += sweepAngle;
        }
      }
    
      protected void newSliceArray(int n) {
        slices = new Slice[n];
      }
    
    
      protected Slice newSlice(float startAngle) {
        return new Slice(startAngle);
      }
    
      @Override
      public String toString() {
        String t = "";
        for (Slice s : slices) {
          t += s.toString();
        }
        return t;
      }
    
      // Inner class...
      public class Slice {
        public Slice(float startAngle) {
          // set some private fields based on startAngle and generic pie
        }
    
        @Override
        public String toString() {
          return "" + a;
        }
      }
    }
    

    ApplePie.java:

    public class ApplePie extends Pie {
      private int b = 2;
    
      // protected Slice[] slices;
    
      // Apple Pie constructor
      public ApplePie(int n) {
        super(n);
      }
    
      protected void newSliceArray(int n) {
        slices = new Slice[n];
      }
    
      protected Slice newSlice(float startAngle) {
        return new Slice(startAngle);
      }
    
      // 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 b + "";
        }
      }
    }
    

    Test:

    public static void main(String[] args) {
        ApplePie ap = new ApplePie(8);
        System.out.println(ap.toString());
    }
    

    The code will print 22222222

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying understand ViewModels deeper and I have read many articles and blogs
trying to understand methods and virtual function lets say i have 3 classes (the
Trying to understand the relationship between UIView and CALayer. I read Apple documentation but
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I have a legacy code to maintain and while trying to understand the logic
I am trying understand how multi queries work in mysqli. But I confess that
I am relatively new java. I am trying understand what are the usage of
Trying to understand a question I got wrong on a test: How does inheritance
I'm trying understand how try ... catch construction works in T-SQL. So I've read
I'm currently learning C++ (Coming from Java) and I'm trying to understand how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.