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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:22:11+00:00 2026-05-23T10:22:11+00:00

EDIT : see bottom First off I searched for an answer before asking this

  • 0

EDIT : see bottom

First off I searched for an answer before asking this one, but as you can see with the title I have no idea how this is called and I will edit the question whenever I can.
Please forgive me on this.

I have the following abstract class :

public abstract class ValidableDTO implements Serializable {
    public abstract boolean isValid();
    public abstract boolean equals(ValidableDTO compared);
    // EDIT : new solution
    public abstract <T extends ValidableDTO> boolean equals(T compared);
}

I’d like to get a similar implementation :

public class MyDTO extends ValidableDTO {

    private String myValue; //With a getter and setter of course

    public MyDTO() {
        // ...
    }

    @Override
    public boolean isValid() {
        return true; // Validation
    }

// __________ LOOK AT THE FOLLOWING PARAMETER TYPE __________­­
    @Override
    public boolean equals(MyDTO compared) {
        return true; // Comparison
    }
}

The closest I could get is

@Override
public boolean equals(ValidableDTO compared) {
    boolean isEqual = false;

    if (compared instanceof MyDTO) {
        isEqual = getValue().equals(compared.getValue());
    }

    return isEqual;
}

I tried using public abstract boolean equals(<? extends ValidableDTO> compared); but this doesn’t work.

Is that even possible (it should be IMO) ? Thank you for your time and … I still don’t know how to describe this in 1 sentence… (lol)

Sincerely.
– me

One step closer, thanks to user : aps !

the following works in the Abstract class definition (ValidableDTO)

public abstract <T extends ValidableDTO> boolean equals(T compared);

__BUT__

the implementation in MyDTO still isn’t fine and in the end, it’s exactly the same as using my ‘if instanceof’ solution because ValidableDTO is an abstract class and HAS to be inherited.

What it looks like for now using Aps’ solution :

public <T extends ValidableDTO> boolean equals(T compared) { ... }

I still have to check if it’s a MyDTO instance…

ON A SIDE NOTE, google doesn’t seem to know if “Validable” or “Validatable” are real words.. which one is correct?

  • 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-23T10:22:11+00:00Added an answer on May 23, 2026 at 10:22 am

    You can achieve that using generics on the declaration of your method signature at the abstract class:

    public abstract class ValidableDTO<T extends ValidableDTO> implements Serializable {
        public abstract boolean isValid();
        public abstract boolean equals(T compared);
    }
    
    public class MyDTO extends ValidableDTO<MyDTO> {
    
        @Override
        public boolean isValid() {
            ...
        }
    
    
        @Override
        public boolean equals(MyDTO compared) {
            return true; // Comparison
        }
    }
    

    [EDIT]

    Summary of the comments discussion below:

    Regarding Java coding style and principles, it would be more reasonable to override the default public boolean equals(Object other) method instead of using a generics construction to try to constrain the type of the parameter. The standard practice for equals(…) is to use the instaceof operator to check for parameter compatibility, perform a cast and use finer-grained comparisons at the level of the specific object structure. That’s similar to the proposed alternative implementation in the original post.

     @Override
    public boolean equals(Object other) {
        if (this == other) return true;
        if (other instanceof MyDTO) { 
            MyDTO otherDTO = (MyDTO) other; 
            return this.getValue().equals(otherDTO.getValue()); // mind null values
        } 
        return false; 
    }
    

    This approach has the additional value of making the class ready for use in a collection, which is a very common case for DTO classes (e.g. List<UserDTO>)

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

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
Edit : See my full answer at the bottom of this question. tl;dr answer
I have this code: http://jsfiddle.net/5RbrL/ As you can see, the text doesn't go over
see the example..the footer won't stay at bottom/ EDIT Tried to remove clearfix but
EDIT: See my working code in the answers below. In brief: I have a
I receive this message (see image below) when I try to edit in debugging.
Preserved question - see Edit at the bottom I'm working on a small functional
Update Added jsfiddle - see bottom of post I currently have a function that
EDIT The bare-bones version of this question is, if I have some object o
EDIT: I would really like to see some general discussion about the formats, their

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.