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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:45:35+00:00 2026-05-17T06:45:35+00:00

The code below is a simplified version of the pattern my project is using.

  • 0

The code below is a simplified version of the pattern my project is using. The standard pattern we use is to have a Writer for each object type. For the subtypes of one abstract type (in this example Animal), I’d like an enum to serve as a lookup for the correct writer.

abstract class Writer<T> {
    abstract void write(T value);
}

abstract class Animal {
    abstract AnimalType getType();
}

class Cat extends Animal {
    AnimalType getType() { return AnimalType.CAT; }
}

class CatWriter extends Writer<Cat> {
    void write(Cat value) { }
}

// The AnimalType stores a reference to the correct writer for the Animal subclass
enum AnimalType {
    CAT(new CatWriter());

    Writer<? extends Animal> writer;
    Writer writerThatWorksWithWarning;
    Writer<Animal> writerThatWorksButCantBeAssigned;

    AnimalType(Writer<? extends Animal> writer) {
        this.writerThatWorksWithWarning = writer;
        this.writer = writer;

        // ERROR: Incompatible Types
        this.writerThatWorksButCantBeAssigned = writer;

    }
}

Sample use case:

class Test {
    public static void main(String... args) {
        Animal value = new Cat();

// ERROR: write (capture<? extends Animal) in Writer cannot be applied to (Animal)
        value.getType().writer.write(value);

// WARNING: Unchecked call
        value.getType().writerThatWorksWithWarning.write(value);

// This line works fine here - but can't be assigned above
        value.getType().writerThatWorksButCantBeAssigned.write(value);
    }
}

I think that my problem is similar to the problem in this question: Java Generics with wildcard, however I can’t tell how to solve it.

I’ve put the inline errors and warnings I get in the comments.

Any ideas?

  • 1 1 Answer
  • 2 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-17T06:45:35+00:00Added an answer on May 17, 2026 at 6:45 am

    Try this instead,

    enum AnimalType {
        CAT(new CatWriter());
    
        private Writer<? extends Animal> writer;
    
        AnimalType(Writer<? extends Animal> writer) {
            this.writer = writer;
        }
    
        public Writer<Animal> getWriter() {
            return (Writer<Animal>)writer;
        }
    }
    

    Moreover, I am not sure what are you up to. But I believe that Visitor pattern will come handy in this case.

    Problem with the above solution, the code below will break the thing.

        Animal cat = new Cat();
        Animal dog = new Dog();
    
        cat.getType().getWriter().write(cat);
    
        // java.lang.ClassCastException in the write() method's argument
        cat.getType().getWriter().write(dog);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method (the code below is a simplified version) that parses small
The code below is a simplified version of my website. On my site, the
Ok, here is setup. Note the code below is SIMPLIFIED version. PHP 5.2 ENGINE:
Below is a simplified version of a segment of code that I'm working on
A simplified version of the code is posted below (white space, comments, etc. removed
I have two data structure classes (this is a simplified version of my code)
The code below is a simplified version of my task bar icon class, I
Below is a simplified version of code I've written to override a class method
Below, is a simplified version of my App , the following code works as
I've simplified my JavaScript code to the example below; this code is giving me

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.