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

  • Home
  • SEARCH
  • 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 8721033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:09:26+00:00 2026-06-13T07:09:26+00:00

How can I have several extensions of a class Fruit , and based on

  • 0

How can I have several extensions of a class Fruit, and based on what class/type the object ist, execute the same method on this fruit but with different function?

Example: I’m using a FruitManager to add a new Fruit fruit = new Apple(); to the fruitstore. If this fruit is an apple, I want of course to add this to the apples list. Elso to bananas list.
Now if I have 10 sorts of fruits, I do no want to create 10 functions like addBanana(), addApple() and so on.
And I too do not want to have cluttering if-else statements for getting the right fruit list.

Can I samehow get the fruitlist just based on the type of object I’m adding?

class Fruit;
class Apple extends Fruit;
class Banana extends Fruit;

class FruitStore {
    List<Fruit> apples = new ArrayList<Apple>();
    List<Fruit> bananas = nwe ArrayList<Banana>();
}

class FruitManager {
    FruitStore store;

    //called from somewhere with Fruit fruit = new Apple();
    addFruit(Fruit fruit) {
        //how could things like this be done in one statement?
        store.<get list apples or bananas>.add(fruit);
    }
}
  • 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-13T07:09:27+00:00Added an answer on June 13, 2026 at 7:09 am

    Making my kludge into an answer. Consider using a HashMap of HashMap<Class<? extends Fruit>, List<Fruit>>. Something like this:

    class FruitStore {
       private Map<Class<? extends Fruit>, List<Fruit>> fruitMap = 
             new HashMap<Class<? extends Fruit>, List<Fruit>>();
    
       public void addFruit(Fruit fruit) {
          Class<? extends Fruit> fruitClass = fruit.getClass();
          List<Fruit> fruitList = fruitMap.get(fruitClass);
          if (fruitList == null) {
             fruitList = new ArrayList<Fruit>();
             fruitMap.put(fruitClass, fruitList);
          }
          fruitList.add(fruit);
       }
    
       public void displayStore() {
          for (List<Fruit> fruitList : fruitMap.values()) {
             System.out.println(fruitList);
          }
       }
    }
    

    I tested this with:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class FruitManager {
       public static void main(String[] args) {
          FruitStore fruitStore = new FruitStore();
          for (int i = 0; i < 20; i++) {
             if (i % 3 == 0) {
                fruitStore.addFruit(new Banana());
             } else {
                fruitStore.addFruit(new Apple());
             }
          }
    
          fruitStore.displayStore();
       }
    }
    
    abstract class Fruit {
       public abstract String getName();
    
       @Override
       public String toString() {
          return getName();
       }
    }
    
    class Apple extends Fruit {
    
       private static final String NAME = "Apple";
    
       @Override
       public String getName() {
          return NAME;
       }
    
    }
    
    
    class Banana extends Fruit {
    
       private static final String NAME = "Banana";
    
       @Override
       public String getName() {
          return NAME;
       }
    
    }
    
    class FruitStore {
       private Map<Class<? extends Fruit>, List<Fruit>> fruitMap = 
             new HashMap<Class<? extends Fruit>, List<Fruit>>();
    
       public void addFruit(Fruit fruit) {
          Class<? extends Fruit> fruitClass = fruit.getClass();
          List<Fruit> fruitList = fruitMap.get(fruitClass);
          if (fruitList == null) {
             fruitList = new ArrayList<Fruit>();
          }
          fruitList.add(fruit);
          fruitMap.put(fruitClass, fruitList);
       }
    
       public void displayStore() {
          for (List<Fruit> fruitList : fruitMap.values()) {
             System.out.println(fruitList);
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several textboxes where users can enter information into them. This can include
We have several areas where code like this can be seen public Map extractData(ResultSet
I have several members in my class which are const and can therefore only
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
i have some Orders that can have several Items and these Items have an
I have a rails 3.2.2 app with patients who can have several types of
I have the following relation in my model: A user can have several consumers
In .NET Compact Framework a device can have several IP Addresses. I want to
I have several sites where users can send me email through an html form
I have several large CSS files and making a change can sometimes take a

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.