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 6819611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:19:37+00:00 2026-05-26T21:19:37+00:00

This is a nasty problem, and it might be that the design is just

  • 0

This is a nasty problem, and it might be that the design is just bad.

Writing a set of simple charts components (pie, bar & line charts) and am choking on some generics stuff. In advance, I’m sure there are many Java APIs for doing exactly what I’m trying to do here (charting/reports/etc.), however I’m interested in this as a general generics problem; the fact that it involves charts & reporting components is trivial.

Every chart inherits from a generic abstract base class Chart:

public abstract class Chart<T extends ChartComponent>
{
    private List<T> components;

    // ...rest of the Chart class
}

The reason why we have T extends ChartComponent is because every chart subclass will be comprised of 1+ so-called chart components (bars, lines, pie wedges, etc.):

public abstract class ChartComponent
{
    private Color color;

    // .. rest of ChartComponent class
}

public class PieWedge extends ChartComponent
{
    double wedgeValue;

    // ... rest of PieWedge class
}

Putting this design together:

public class PieChart extends Chart<PieWedge>
{
    // ... thus its list of ChartComponents is actually a List<PieWedge>
}

This way, PieChart isn’t generic (nor should it be) and is always of type Chart<PieWedge>.

I previously had the same setup for bar and line charts, which were defined as BarChart extends Chart<BarGroup> and LineChart extends Chart<Line> respectively (since a bar chart consists of 1+ groups of bars, and a line chart consists of 1+ lines).

Now I want to abstract Bar and Line charts out even further. Both of these charts are actually plotted against an (x,y) Cartesian graph with x- and y-axes; this is as opposed to a pie chart which is not plotted against any such axes.

Ideally, I wanted to create a new abstract class called CartesianChart which extended Chart, and then have BarChart and LineChart both extend CartesianChart. This new CartesianChart would introduce new properties (xAxisLabel, gridTurnedOn, etc.) that logically apply to bar/line charts but not pie charts.

Furthermore, to restrict CartesianChart so that it could only have chartComponents of type BarGroup or Line (and not PieWedge), I would like to create a new chart component type like CartesianComponent extends ChartComponent, and then have BarGroup/Line extend that. Doing so would prevent code like this from compiling:

LineChart lineChart = new LineChart();
lineChart.addLine(new PieWedge());

Since Line extends CartesianComponent, but PieWedge only extends ChartComponent. Thus, before getting to my problem we have the following inheritance hierarchy:

Chart
    CartesianChart
        BarChart
        LineChart
    PieChart

ChartComponent
    CartesianComponent
        BarGroup
        Line
    PieWedge

PieChart extends Chart<PieWedge>

CartesianChart extends Chart<CartesianComponent>

BarGroup extends CartesianComponent
Line extends CartesianComponent

BarChart extends CartesianChart<BarGroup>
LineChart extends CartesianChart<Line>

The problem with this setup is that on both BarChart and LineChart it gives a compiler error complaining that CartesianChart is not generic. This makes complete sense, but I’m not sure what I can do to fix it!

If I try to re-define CartesianChart:

public abstract class CartesianChart<T extends CartesianComponent> extends Chart<CartesianComponent>
{
    // ...
}

I get “type mismatch” compiler errors all through my bar/line chart code. In every instance of the error, it states that it is expecting arguments of type List<CartesianComponent> but instead found List<BarGroup> or List<Line> and that they are not suitable substitutes.

Hopefully, this is a quick fix somewhere in the class definition of CartesianChart and/or CartesianComponent. Otherwise I may have to re-design the entire chart library. Either way, I’m interested in any and all suggestions, except ones like “Hey, why don’t you just try JFreeCharts or …”. Again, I’m interested in the solution here as it related to solving a broad range of similar generics problems; the fact that this involves reporting/charting is trivial.

Thanks in advance for any and all help!

  • 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-05-26T21:19:37+00:00Added an answer on May 26, 2026 at 9:19 pm

    Your Chart class contains the List<T> that you speak of, so when you you define your CartesianChart abstract class to extend Chart<CartesianComponent>, you are saying that List<T> is really List<CartesianComponent>.

    Really, what you want is to just use the generic as you defined it in your abstract class (that is, <T extends CartesianComponent>). I would try doing this and see how it works out.

    public abstract class CartesianChart<T extends CartesianComponent> extends Chart<T>
    {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this nasty problem where sending multiple, large messages in quick succession from
I had this nasty bug that disappeared in the past but now after quite
Hey. I've got a dumb, but nasty problem. If I've got this (simplified) situation:
Maybe this is just a stupid idea, that's why I toss it in here:
I have a really nasty problem with bindings. I know that there are other
My team and I have this nasty problem with parsing a string received from
I'm having a really nasty problem with some code that I've written. I found
I'm writing a simple memory arena allocator and facing a small problem with exception
I'm fighting with this nasty problem. I've developed a page template for my website
This is a nasty one for me... I'm a PHP guy working in Java

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.