D
D
DR_Demons2018-03-25 11:20:01
Java
DR_Demons, 2018-03-25 11:20:01

How compares java objects when using lombok?

Good day to all! I encountered strange behavior, I can’t understand what’s wrong, in theory the code is similar, but it works differently.
There is an abstract class AbstractCat

package animal;


import lombok.Data;

@Data
public abstract class AbstractCat {

    private String name;
    private Integer age;

}

he has an heir Cat
package animal;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Cat extends AbstractCat {
    private String say;
}

similarly, they have the AbstractTransport class, its successor Transport
package transport;

import lombok.Data;

@Data
public abstract class AbstractTransport {
    private Integer rnum;
    private String rtype;
}

package transport;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Transport extends AbstractTransport {

    private Integer arrt;
    private String where;
    private String vehid;
    private Integer rid;
    private String lastst;

}

The boilerplate in each class is generated using lombok, respectively, the equls and hashcode methods are overridden by it, so the parent.equals(child) is true, but for some reason, when compared in the case of Cat and AbstractCat, they are true, and Transport and AbstractTransport are false, although the classes are similar why such behaviour?
import animal.AbstractCat;
import animal.Cat;
import transport.AbstractTransport;
import transport.Transport;

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        animal();
        System.out.println();
        transport();
    }

    private static void animal() {
        AbstractCat abstractCat = new AbstractCat() {
            @Override
            public void setName(String name) {
                super.setName(name);
            }

            @Override
            public void setAge(Integer age) {
                super.setAge(age);
            }
        };

        abstractCat.setAge(1);
        abstractCat.setName("Cat");

        Cat cat = new Cat();
        cat.setAge(1);
        cat.setName("Cat");
        cat.setSay("Hello");

        System.out.println("abstractCat.equals(cat) " + abstractCat.equals(cat));

    }

    private static void transport() {

        AbstractTransport abstractTransport2 = new AbstractTransport() {
            @Override
            public void setRnum(Integer rnum) {
                super.setRnum(rnum);
            }

            @Override
            public void setRtype(String rtype) {
                super.setRtype(rtype);
            }
        };

        abstractTransport2.setRnum(33);
        abstractTransport2.setRtype("М");

        Transport transport1 = new Transport();
        transport1.setRnum(33);
        transport1.setRtype("M");
        transport1.setWhere("dudl");

        System.out.println("abstractTransport.equals(transport1) " + abstractTransport2.equals((Transport)transport1));
        System.out.println("transport1.equals(abstractTransport) " + transport1.equals(abstractTransport2));

    }
}

Conclusion:
abstractCat.equals(cat) true

abstractTransport.equals(transport1) false
transport1.equals(abstractTransport) false

Tell me, who knows?
Thanks in advance!
└── src
    ├── main
    │   ├── java
    │   │   ├── animal
    │   │   │   ├── AbstractCat.java
    │   │   │   └── Cat.java
    │   │   ├── Main.java
    │   │   └── transport
    │   │       ├── AbstractTransport.java
    │   │       └── Transport.java
    │   └── resources
    └── test
        ├── java
        └── resources

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DR_Demons, 2018-03-25
@DR_Demons

The issue is resolved, the reason is a banal inattention, the whole problem was that the Rtype "M" was in a different layout))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question