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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:50:53+00:00 2026-05-26T05:50:53+00:00

Say I have a generic array like this: ArrayList<Fruit> fruits = new ArrayList<Fruit>(); Then

  • 0

Say I have a generic array like this:

ArrayList<Fruit>  fruits = new ArrayList<Fruit>();

Then I add lots of different fruits, which all extends the class fruit, and loop through them

for (Fruit f : fruits) {

}

If the fruit is a banana, I want to check how round it is, so..

for (Fruit f : fruits) {
    if (f instanceof Bannana)
        f.checkHowRoundBannanaIs();
}

I will have to put the checkHowRoundBannnaIs() method in the fruit class, even though the fruit may not be a banana, because I can’t use the function on f if it isn’t in the fruit class, otherwise I get a undefined method error (or something like that).

This is fine for one or two methods, but after a while it makes the class bulky and ugly. So my question is, how can I add methods to a class and use them in a for-style loop of its super class?

  • 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-26T05:50:54+00:00Added an answer on May 26, 2026 at 5:50 am

    So my question is, how can I add methods to a class and use them in a for-style loop of its super class?

    Short answer, you can’t really.

    If you want to call the methods in the body of the for-loop, instanceof and down-casting is the only way I can think of:

    for (Fruit f : fruits) {
        if (f instanceof Banana)
            ((Banana) f).checkHowRoundBananaIs();
        // ...
    }
    

    Both instanceof and down-casting are usually considered bad practice though. To avoid it, you could implement the visitor pattern. Here are the steps you would need to take:

    1. Create a visitor interface like this:

      interface FruitVisitor {
          void visit(Banana banana);
          void visit(Apple apple);
      }
      
    2. Let all fruits accept a visitor:

      abstract class Fruit {
          // ...
          public abstract void accept(FruitVisitor fv);
      }
      
      class Banana extends Fruit {
          public void accept(FruitVisitor fv) {
              fv.visit(this);
          }
      
          public void checkHowRoundBananaIs() { ... }
      }
      
    3. Create a visitor that takes the appropriate action for each type of fruit, and pass it as argument to the accept method of each fruit in your list:

      FruitVisitor fv = new FruitVisitor() {
          public void visit(Banana banana) {
              banana.checkHowRoundBananaIs();
          }
      
          public void visit(Apple apple) {
              // ...
          }
      };
      
      for (Fruit f : fruits)
          f.accept(fv);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that i have a generic dictionary with data like this (I hope the
Which is your preference? Let's say we have a generic Product table that has
Lets say you have a relational DB table like INVENTORY_ITEM. It's generic in the
I have a generic class defined like this class MyGenericClass<T> where T : ICompareable
Let's say I have a generic pointer in objective-c. This pointer could either be
Say I have a generic method with multiple type constraints, this this: public static
Let's say I have a generic member in a class or method, like so:
Let's say I have a generic Object class, and a generic List class. I
I have a two generic abstract types: Entity and Association . Let's say Entity
Lets say have this immutable record type: public class Record { public Record(int x,

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.