Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you declare an abstract method so that the parameter type (class) is always the Children class?

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?

like image 534
dominicbri7 Avatar asked Jan 20 '23 08:01

dominicbri7


1 Answers

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>)

like image 98
maasg Avatar answered Jan 23 '23 05:01

maasg