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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:50:45+00:00 2026-06-06T12:50:45+00:00

Reusing a previous example, imagine a zoo where newly arriving animals have to be

  • 0

Reusing a previous example, imagine a zoo where newly arriving animals have to be “processed” by the Zookeeper (think of checking them in to the zoo). Each animal’s check-in process depends on its class taxonomy (Mammal, Bird, etc).

The processes differ because of the fundamental differences between the taxonomy classes – for example, Birds have wings, Mammals have teeth. You might also have some shared bits of the process which are true for all animals, but I’ve omitted them here.

Here is the code:

Animal.java

public interface Animal {

    public AnimalProcessor<? extends Animal> getAnimalProcessor();
}

Mammal.java

public abstract class Mammal implements Animal {

    @Override
    public AnimalProcessor<Mammal> getAnimalProcessor() {

        return new MammalProcessor();
    } 

    // Specific to mammals
    public abstract int getToothCount();

}

Bird.java

public abstract class Bird implements Animal {

    @Override
    public AnimalProcessor<Bird> getAnimalProcessor() {

        return new BirdProcessor();
    }

    // Specific to birds
    public abstract float getWingspan();
}

AnimalProcessor.java

public interface AnimalProcessor<T extends Animal> {

    public void process(T critter);
}

MammalProcessor.java

public class MammalProcessor implements AnimalProcessor<Mammal> {

    @Override
    public void process(Mammal a) {
        System.out.println("Tooth count is " + a.getToothCount());
    }

}

BirdProcessor.java

public class BirdProcessor implements AnimalProcessor<Bird> {

    @Override
    public void process(Bird a) {
        System.out.print("Wingspan is " + a.getWingspan());
    }

}

Badger.java

public class Badger extends Mammal {

    @Override
    public int getToothCount() {
        return 40;
    }
}

Condor.java

public class Condor extends Bird {

    @Override
    public float getWingspan() {
        return 2.9f;
    }
}

ZooKeeper.java

import java.util.List;

public class ZooKeeper {

    public void processNewAnimals(List<Animal> newcomers)
    {
        for(Animal critter : newcomers)
        {
            AnimalProcessor<? extends Animal> ap = critter.getAnimalProcessor();

                        // This line has a compilation error!
            ap.process(critter);
        }
    }
}

MainClass.java

import java.util.LinkedList;
import java.util.List;

public class MainClass {

    public static void main(String[] args) {

        ZooKeeper keeper = new ZooKeeper();

        List<Animal> animals = new LinkedList<Animal>();

        animals.add(new Badger());
        animals.add(new Condor());

        keeper.processNewAnimals(animals);

    }
}

There are no warnings anywhere, but ap.process(critter) cannot compile.
I know this is because AnimalProcessor<Bird> is not of type AnimalProcessor<Animal> but I cannot see how resolve the issue. The call to <T extends Animal> getAnimalProcessor() will return a suitable AnimalProcessor<T extends Animal>, but I cannot express this in the code.

Perhaps I should not be pulling the AnimalProcessor out in the first place?

The goal is, of course, to be able to add Reptile, without changing the core.

  • 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-06T12:50:47+00:00Added an answer on June 6, 2026 at 12:50 pm
    public interface Animal<THIS extends Animal<THIS>> {
        AnimalProcessor<THIS> getAnimalProcessor();
    }
    public abstract class Mammal implements Animal<Mammal> {
    

    A little like Enum<E extends Enum<E>>.

    It may be a better idea to remove the method.

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

Sidebar

Related Questions

I have the following 4 lines of code that I keep reusing in my
I am doing some C programming for school and I have found myself reusing
I have an issue where I'm reusing the dialog so I wouldnt have to
I have several scripts that could be reusing variables so I'd like to isolate
I have a .cs file full of C# code I keep reusing in multiple
I have a View Controller I'm reusing to edit individual objects (NSString, NSNumber, NSDate)
When reusing code (for example, a Util library you created), do you add its
I have a view which I'm reusing (its a date range selection screen) it
I've tried two different methods of reusing code. I have a solution full of
In the interest of reusing some existing code that was defined as an instance

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.