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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:29:40+00:00 2026-06-14T12:29:40+00:00

I have a generic class with parameter that extends Paint. I really don’t understand

  • 0

I have a generic class with parameter that extends Paint. I really don’t understand why I should cast it manually to T in first constructor. What am i doing wrong? Or this is the case when the compiler can’t determine a safe cast itself?

public class XYPlot <T extends Paint> extends AbsPlot implements XYChartElement {
public XYPlot(AbsSeries series){
    setUp(series, (T) new Paint(DEFAULT_PAINT));//TODO
}

public XYPlot(AbsSeries series, T paint){
    setUp(series, paint);
}

private void setUp(AbsSeries series, T paint){
    if(series == null) throw new NullPointerException("Series is null");
    setSeries(series);
    setPaint(paint);
}
  • 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-14T12:29:41+00:00Added an answer on June 14, 2026 at 12:29 pm

    I really don’t understand why I should cast it manually to T in first constructor.

    You shouldn’t – you shouldn’t be creating an instance of just Paint in the first place. That Paint won’t be an instance of T, unless T is exactly Paint. A generic class which only works properly for a single type argument shouldn’t be generic in the first place.

    If you need an instance of T on construction, you’ll either need the caller to pass one in, or take a Class<T> so that you can look through the constructors using reflection and call an appropriate one.

    Let’s look at a simpler version of what you’re doing, and hopefully you’ll see why it’s wrong:

    public class Wrapper<T extends Object>
    {
        private final T value;
    
        public Wrapper()
        {
            value = (T) new Object();
        }
    
        public T getValue()
        {
            return value;
        }
    }
    

    Here we’re using Object instead of Paint – but otherwise, it’s basically similar.

    Now if we call it:

    Wrapper<String> wrapper = new Wrapper<String>();
    String text = wrapper.getValue();
    

    … what would you expect that to do?

    Fundamentally it’s not clear why you have made your class generic in the first place – but the approach you’re taking is inherently flawed.

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

Sidebar

Related Questions

supposed we know that ViewGroup extends View. Further we have a generic, parametrized class
There is a generic method that takes a class as parameter and I have
I have a Java class that takes a generic type parameter and a class
I have a generic class that needs to limit an enum depending on the
I have a generic class that takes a type T . Within this class
If have a type hierarchy that starts out with a generic type class A<T>
I have a class that extends a base class. The base class is more
I have public class First<T> {} public class Second<T extends SomeConcreteClass> extends First<T> {}
I have a generic list class that implements a particular interface. The list items
I have a generic class HierarchicalBusinessObject. In the constructor of the class I pass

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.