Claud
Claud

Reputation: 1075

Combine two classes in Java?

I don't know what the right way to ask this but I have two classes I get back from the API that both have name as the thing in common but also have another field that is separate from each other. What is the best way to "combine" them where my User class can get the name but can also get their own variables.

How would my User class look? I'm looking for the User class to have name in-common, the Alien class have alienVariable and the Human class have the humanVariable

My example of two classes

Alien

@AutoValue
public abstract class Alien implements Parcelable {
    public static Alien create(String alienVariable) {
        return new AutoValue_Alien(alienVariable);
    }

    public static Alien create(String name, String alienVariable) {
        return new AutoValue_Alien(name, alienVariable);
    }

    public static TypeAdapter<Alien> typeAdapter(Gson gson) {
        return new AutoValue_Alien.GsonTypeAdapter(gson);
    }

    @SerializedName("name")
    public abstract String name();

    @Nullable
    @SerializedName("alien_variable")
    public abstract String alienVariable();
}

Human

@AutoValue
public abstract class Human implements Parcelable {
    public static Human create(String humanVariable) {
        return new AutoValue_Human(humanVariable);
    }

    public static Human create(String name, String humanVariable) {
        return new AutoValue_Human(name, humanVariable);
    }

    public static TypeAdapter<Human> typeAdapter(Gson gson) {
        return new AutoValue_Human.GsonTypeAdapter(gson);
    }

    @SerializedName("name")
    public abstract String name();

    @Nullable
    @SerializedName("human_variable")
    public abstract String humanVariable();
}

User?

Upvotes: 1

Views: 13404

Answers (3)

Claud
Claud

Reputation: 1075

What I was looking for was what Vahid suggested. I didn't want inheritance but polymorphism. (should've specified)

Just made an interface class that looks like this.

public interface Species {
    String name();
}

And then just override it in each class, example for the Human class

@AutoValue
public abstract class Human implements Parcelable, Species {
    public static Human create(String humanVariable) {
        return new AutoValue_Human(humanVariable);
    }

    public static Human create(String name, String humanVariable) {
        return new AutoValue_Human(name, humanVariable);
    }

    public static TypeAdapter<Human> typeAdapter(Gson gson) {
        return new AutoValue_Human.GsonTypeAdapter(gson);
    }

    @SerializedName("name")
    public abstract String name();

    @Nullable
    @SerializedName("human_variable")
    public abstract String humanVariable();

    @Override
    public String name() {
       return name();
    }
}

Upvotes: 0

paul_hundal
paul_hundal

Reputation: 381

You can do something like the following. BTW, I haven't tried to compile this it's just a scaffold of an idea

public interface Species implements Parcelable {
    @SerializedName("name")
    String name();
    @Nullable
    String typeVariable();
}

public class Alien implements Species {
    @Override
    public String name() {}

    @Override
    @SerializedName("alien_variable")
    public String alienVariable() {}

    // fill in rest of class
}

public class Human implements Species {
    @Override
    public String name(){}

    @Override
    @SerializedName("human_variable")
    public String humanVariable() {}

    // fill in rest of class
}

Upvotes: 1

Vahid Hashemi
Vahid Hashemi

Reputation: 5240

use an interface with name() method in it.

Generally speaking if you have some common properties its better to have a super class rather than interfaces but if fulfill the super class slot then your only choice is interface.

Upvotes: 1

Related Questions