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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:58:38+00:00 2026-05-29T04:58:38+00:00

A and AService are base classes. B and BService extend these classes. A and

  • 0

A and AService are base classes.

B and BService extend these classes.

A and B are beans containing parameters for the services.

BService expects a B typed argument in the execute method.

public class A
{
    private int a1;

    public int getA1() { return a1; }
    public void setA1(int a1) { this.a1 = a1; }
}

public class B extends A
{
    private int b1;

    public int getB1() { return b1; }
    public void setB1(int b1) { this.b1 = b1; }
}

public abstract class AService
{
    public int execute(A a)
    {
        return a.getA1() + getValue();
    }

    public abstract int getValue(A a);
}

public class BService extends AService
{
    public int getValue(A a)
    {
        B b = (A) a;

        return b.getB1();
    }
}

Is there a better way to do this code ?
In particular, is there a way to avoid to cast objects ?

  • 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-29T04:58:39+00:00Added an answer on May 29, 2026 at 4:58 am

    It sounds like generics are what you’re looking for. Typically, whenever you have a concrete class which can always safely cast a value, you can usually express this via generic parameters (and have it checked at compile time).

    In this particular example, you’d declare the AService with a generic parameter which must be some subclass of A. Then you use that parameter to make some methods specific to the particular type – in this case the getValue method, as something like

    public class AService<T extends A> {
    
       // Now this takes a T - i.e. the type that a subclass is parameterised on
       public abstract int getValue(T a)
    
       // Execute will have to take a T as well to pass into getValue - an A
       // wouldn't work as it might not be the right type
       public int execute(T a)
       {
          return a.getA1() + getValue(a);
       }
    }
    

    where the T is a type parameter (conventionally a single uppercase letter). Then you can declare the BService as

    public class BService extends AService<B> {
    
       // The type is checked by the compiler; anyone trying to pass an instance
       // of A into this class would get a compile-time exception (not a class cast
       // at runtime)
       public int getValue(B b) {
           return b.getB1();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base service class, and I have a bunch of services, is
I have an application that tracks services (servers) with IP addresses. I'm trying to
I created a simple RESTful WCF service with a single method for receiving files
I have a WCF solution that consists of the following class libraries: Exercise.Services: Contains
I've got an HTTP service I defined in Flash Builder, via the Data Services
I have a base controller that I made so I can pass data to
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes
I have inherited a code-base which uses a compiled logging library. I cannot update
I have a service-layer Interface that extends from a base Interface; I would like
The code that sparked this question was a Service in my company's code base

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.