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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:19:56+00:00 2026-06-12T23:19:56+00:00

Here is an abstract example of my problem: I have a general class (

  • 0

Here is an abstract example of my problem: I have a general class (Car) which has a type (brand). All objects should only differ by brand, and based on this brand should be handled differently.

All objects of this class are collected in a List of a Service class. The service should perform a routine on the whole list, which is mostly the same. Just one function call in between should differ.

Based on this type I want to call different methods: At the moment I’m asserting equals for the enum type and call different methods based on the outcome. But this is kind of ugly and I wonder if there are better solutions on this?

class Car {
    public enum Brand {
        BMW, AUDI;
    }

    private Brand brand;

    specificMeth1();
    specificMeth2();
}


class Service {

    List<Car> bmw;
    List<Car> Audi;

    processCar() {
        processList(bmw);
        processList(audi);
    }

    processList(List<Car> list) {
        for (Car car : list) {
            if (car.getBrand.equals(Brand.BMW)) {
                specificMeth1();
            } else {
                specificMeth2();
            }
        }
    }
}
  • 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-12T23:19:58+00:00Added an answer on June 12, 2026 at 11:19 pm

    You should put the method in the enum:

    public enum Brand {
        BMW {
            @Override
            public void doSomething();
        },
        AUDI {
            @Override
            public void doSomething();
        };
    
        public abstract void doSomething();
    }
    
    var.getBrand().doSomething();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All right. The problem here is pretty abstract. Bear with me. I have a
I have an abstract Handle<T> class that contains references an objects of type T.
Here, I have an abstract class: abstract class A<E extends A> { abstract void
Here is the problem: I have a representation of a tree. let's abstract this
I have two classes that are derived from an abstract generic class, which is
To illustrate my point, here an example: abstract class Wrapper[A](wrapped: A) { protected def
Here's the situation: I have an abstract class with a constructor that takes a
I have an abstract base class which acts as an interface. I have two
My first question here... I want to use an abstract class A from which
My problem is almost exactly the same as the problem posted here: Abstract class

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.