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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:31:33+00:00 2026-06-15T15:31:33+00:00

I try to implement a fluent interface in my 2D game engine. Simplified example

  • 0

I try to implement a fluent interface in my 2D game engine.

Simplified example of my implementation:

public class Sprite<T> {

    protected float x = 0.0;
    protected float y = 0.0;

    public T setPosition(float x, float y) {
        this.x = x;
        this.y = y;
        return (T)this;
    }

}

public class Living<T extends Living> extends Sprite<Living> {

    protected boolean alive = false;

    public T setAlive(boolean alive) {
        this.alive = alive;
        return (T)this;
    }

}

public class Entity<T extends Entity> extends Living<Entity> {

    protected String name = null;

    public T setName(String name) {
        this.name = name;
        return (T)this;
    }

}


Entity entity = new Entity().setPosition(100, 200).setAlive(true).setName("Zombie");

I keep getting the error: “The function setAlive(boolean) does not exist.”

I know, using my methods the other way round (in a more logical order) works:

Entity entity = new Entity().setName("Zombie").setAlive(true).setPosition(100, 200);

And I know that overwriting any parent setter functions in each and every child class would work:

public class Entity extends Living {

    protected String name = null;

    public Entity setPosition(float x, float y) {
        return (Entity)super.setPosition(x, y);
    }

    public Entity setAlive(boolean alive) {
        return (Entity)super.setAlive(alive);
    }

    public Entity setName(String name) {
        return (Entity)super.setName(name);
    }

}

But I want the interface to be as free/uncomplicated as possible for the ‘end user’ and the code to be as compact and clean as it gets.

I don’t know if I just messed up the generics or my hole approach is completely wrong. I hope you can help. I am open to any advice. (Sorry for my bad english.)

Edit:
I already tested the following approach and it works for the Entity class.

public class Sprite<T> {
...
}

public class Living<T> extends Sprite<T> {
...
}

public class Entity extends Living<Entity> {
...
}

I forgot to mention, that I need to instantiate Sprite & Living too. For example:

Living living = new Living().setPosition(50, 50).setAlive(false);
  • 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-15T15:31:34+00:00Added an answer on June 15, 2026 at 3:31 pm

    I think your class model is over complicated, you can pass generic parameter down to the child class and then declare it explicitly:

    public class Sprite<T> {
    ...
    }
    
    public class Living<T> extends Sprite<T> {
    ...
    }
    
    public class Entity extends Living<Entity> {
    ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write a (fluent) mapping against an interface public interface IOrderDiscount :
I try to implement the word count example by myself, here's my implementation of
If I try to implement my class on this file I get an error
If I try to implement this simple-as-possible example: Update Content with AJAXRefreshRequest it does
I want to try to implement the Tetris Game using TDD. From what I've
I try to implement the following function : template<typename T> class a { private:
I try to implement callback interface with COM techonlogy and have IDL like that:
I try to implement the following code: <div class=clearid=usernameline>username:<input type=text name=username id=username style=border:1px solid
Public interface IRuleObject {} Public class RuleBase : IRuleObject {} Public class Length :
I try to implement a GType interface in C++ using Glibmm (part of Gtkmm).

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.