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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:26:51+00:00 2026-05-23T01:26:51+00:00

I’m not sure if I convey the question well enough and I couldn’t find

  • 0

I’m not sure if I convey the question well enough and I couldn’t find a better way to do this, as I am quite new to java.

I believe the best is by way of illustration, if I have a class

public abstract class Genome
{
    abstract public Genome randomize();
    abstract public Genome mutate();
    abstract public Genome crossOver(Genome genome);
}

Can I make sure that its subclasses would always implement by using generics

public class GenomeSubclass extends Genome
{
    public GenomeSubclass randomize();
    // etc...
}

instead of the abstract methods provided in the contract?

  • 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-23T01:26:51+00:00Added an answer on May 23, 2026 at 1:26 am

    The normal way would be to define it like this:

    public abstract class Genome<T extends Genome<T>> {
        abstract public T randomize();
        abstract public T mutate();
        abstract public T crossOver(T genome);
    }
    
    public class GenomeSubclass extends Genome<GenomeSubclass>
    {
        public GenomeSubclass randomize();
        // etc...
    }
    

    This is the approach used by Comparable (or at least most uses of Comparable) and Enum, too.

    Of course, this does not avoid subclassing other subclasses.


    Edit to detail my comment:

    You can’t have both

    class GenomeSubclass extends Genome {
        public GenomeSubclass crossOver(GenomeSubclass other);
    }
    

    and

    class GenomeSubSubclass extends GenomeSubclass {
        public GenomeSubclass crossOver(GenomeSubSubclass other);
    }
    

    while both are implementing the same Genome method. Every subtype of GenomeSubSubclass must implement the crossOver(GenomeSubclass) method, and can’t restrain the argument further.

    Of course, you could provide GenomeSubclass with an type parameter of its own:

    public class GenomeSubclass<T extends GenomeSubclass<T>> extends Genome<GenomeSubclass<T>>
    {
        public GenomeSubclass<T> randomize();
        // etc...
    }
    

    but then you can’t really use this class directly without somehow going back to the raw type. (You would have to write GenomeSubclass<GenomeSubclass<GenomeSubclass<...>>>, which is not really possible. Or I’m somehow mis-thinking here.)


    The principle idea here is that every Genome subtype being used as a type parameter T would be compatible to all its own subtypes. This means, we could have this:

    public abstract class GenomeA extends Genome<GenomeA> {}
    

    and then

    public class AImpl1 extends GenomeA {
       public GenomeA randomize();
       public GenomeA mutate();
       public GenomeA crossOver(GenomeA other);
    }
    
    public class AImpl2 extends GenomeA {
       public GenomeA randomize();
       public GenomeA mutate();
       public GenomeA crossOver(GenomeA other);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this

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.