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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:52:06+00:00 2026-06-06T15:52:06+00:00

public class Service{ String serviceName; //setter and getter } public class Version{ int VersionID;

  • 0
 public class Service{ 
       String serviceName;
      //setter and getter
  }

  public class Version{ 
       int VersionID;
      //setter and getter
  }

  public void test(Object list){

        //it shd print the obtained list

   }

List< Service> list1; //Service is a Bean
List< Version> list2; //Version is a Bean
test(list1);
test(list2);

Now the test method shd print the obtained list – (i.e) If the list is of type Service ,then serviceName should be printed using its getter. If the list type is Version versionID should be printed.
Is it possible to achieve this without using Interface or abstract 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-06-06T15:52:08+00:00Added an answer on June 6, 2026 at 3:52 pm

    @danLeon has the simplest idea so far (adding toString to the classes), assuming you have access to Service and Version.

    I’m not sure why you were thinking of reflection, but the only thing I can think of is that you want something that will work with any object that has a single attribute String getter, and then you would do something like this (crazy IMO, but it uses reflection):

    Class clazz = list.get(0).getClass();
    Method[] methods = clazz.getDeclaredMethods();
    Method onlyStringGetter = null;
    for (Method method: methods) {
        String mName = method.getName();
        if (mName.matches("get\w+") {
            if (method.getReturnType().equals(String.class) {
                if (onlyStringGetter != null) thrown new RuntimeException("More than one String getter available");
                onlyStringGetter = method;
            }
        }
    }
    if (onlyStringGetter == null) throw new RuntimeException("No String getter found for class: " + clazz.getName());
    List<String> strings = new ArrayList<String>();
    for (Object singleStringAttribObj: list) {
        // some exception handling needed for below
        String result = (String)onlyStringGetter.invoke(singleStringAttribObj);
        strings.add(result);
    }
    System.out.println(strings);
    

    I haven’t compiled or tried, but that is approximately right. Definitely some additional exception handling is required

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

Sidebar

Related Questions

Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
Consider the following method that stops a service: Public Function StopService(ByVal serviceName As String,
I have this web service in Java: @WebService(serviceName = Catalogo_V1) public class Catalogo_V1 {
I have this web service in Java: @WebService(serviceName = Catalogo_V1) public class Catalogo_V1 {
i have a web service working like this: @WebService(serviceName = TempService) public class TempService
Lets say I have this service: public class Service { ... public Service(IFactory factory)
I have a .NET service, based on ServiceBase : public partial class Service :
I implemented Service for listening user location: public class ListenLocationService extends Service { private
This is my service file: public class SMSCounterService extends Service { /** Called when
I implemented Android service for listening user location: public class ListenLocationService extends Service {

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.