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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:04:10+00:00 2026-06-11T17:04:10+00:00

I have several Java interfaces/ABCs/classes: public abstract class Target { public abstract void fire(Load

  • 0

I have several Java interfaces/ABCs/classes:

public abstract class Target {
    public abstract void fire(Load load);
}

public class HttpTarget extends Target {
    @Override
    public void fire(Load load) {
        // ...
    }
}

public interface Load {
    // ...
}

public class HttpLoad implements Load {
    // ...
}

// Inside a driver
Target target = testSuite.getTarget();
Load load = testSuite.getLoad();

target.fire(load);

So essentially a Target can fire() a Load. My main app Driver doesn’t care about what kind of Target is returned by getTarget(), or what kind of Load is returned by getLoad(). It’s job is to make sure that a load is fired.

I’d like to change the fire() method definition inside HttpTarget to:

@Override
public void fire(HttpLoad httpLoad) {
    // ...
}

However when I do that, Java complains that the method override doesn’t match the definition provided by its parent Target class (as Load and HttpLoad are two different things).

What’s the solution here? Generics? Abstract factories? Ultimately, I want to be able to enforce that HttpTarget‘s fire() method can only accept HttpLoads, but still be compatible with the Driver code. Can someone provide a code example? Thanks in advance!

  • 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-11T17:04:11+00:00Added an answer on June 11, 2026 at 5:04 pm

    Yes, you would need generics:

    public abstract class Target<L extends Load> {
        public abstract void fire(L load);
    }
    
    public class HttpTarget extends Target<HttpLoad> {
        @Override
        public void fire(HttpLoad load) {
            ...
        }
    }
    
    public interface TestSuite<L extends Load> { // or class
        L getLoad();
        Target<L> getTarget();
    }
    
    public class HttpTestSuite implements TestSuite<HttpLoad> {
        @Override
        public HttpLoad getLoad() {
            ...
        }
    
        @Override
        public Target<HttpLoad> getTarget() {
            return new HttpTarget();
        }
    }
    

    The reason Java refuses to compile your HttpTarget class is because it doesn’t override the Target’s fire(Load) method. Indeed, a Target, by contract is supposed to accept any kind of Load as argument. And the HttpTarget‘s fire() method only accepts instances of HttpLoad, and thus breaks the Liskov principle. Generics are the solution to this problem.

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

Sidebar

Related Questions

I have several Abstract Base Classes which act like interfaces as known from Java
I have a quite huge Java class that has several imported packages and libraries
import java.util.Collection; public class Test { public static void main(String[] args) { Collection c
I have an application that uses several different Java classes and would like to
In Java, Can an object have several different classes ? If yes , how
So I have this interface public interface EventHandler<E extends EventObject> { public void handleEvent(E
I have several Java applications. All of them have external jar dependencies and also
I have coded a server in Java that will have several clients connected to
I have tried several ways to login to a website through java. I have
We have a Java client which is using corba to call several third party

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.