0

I'm trying to translate JavaScript code into Java code.

In JavaScript, interfaces can have attributes, and an interface can extend multiple interfaces:

interface Interface1 {
    name?: string
    age?: number
}

interface Interface2 {
    id?: string
}

interface InterfaceCombination extends Interface1, Interface2 {
    position?: string
}

I want to translate this JavaScript code into Java code. I have several ideas, but I don't know if they are good designs.

First idea: classes & delegation

Since Java interfaces only have final static attributes, I choose classes to maintain those attributes:

public class Class1 {
    private String name;
    private Number age;
    public String getName();
    public void setName(String name);
    ... // getter & setter for age
}

public class Class2 {
    private String id;
    ... // getter & setter for id 
}

However, there is the problem. A Java class can only extends one class, so I choose to use delegation:

public class ClassCombination {
    private Class1 class1;
    private Class2 class2;
    private String position;
    public String getName() {
        return class1.getName();
    }
    ... // getters & setters
}

This would lead to a lot of repeated work when the delegation hierarchy is deep. I tried to use @Delegate annotation in lombok, but this is experimental and doesn't support recursive delegation.

Second idea: interface & getters & setters

Though a Java class can only extend one class, a Java interface can extend multiple interfaces.

public interface Interface1 {
   public String getName();
   public void setName();
   ... // getter & setter for age
}

public interface Interface2 {
   ... // getter & setter for id
}

public interface InterfaceCombination extends Interface1, Interface2 {
   ... // getter & setter for position
}

This solution would lead to a heavy work on writing getters and setters in interfaces. And I cannot use lombok to automatically generate getters and setters in interfaces.

Is there any solution more elegant and easier to implement?

  • 2
    Your IDE will write all the getters & setters for you. Not really much work at all. – Basil Bourque Nov 27 '21 at 05:32
  • @BasilBourque Could you tell me which extension for VS Code can do that? – IcePear-Jzx Nov 27 '21 at 06:42
  • You could refactor the classes depending on the actual meaning of your classes. You could prefer composition as in the first approach if Combo class IS-NOT Class1/Class2. If there are any direct relationship, then prefer 2nd approach. Also, you may combine Interface1 and 2 if it is not necessary for those two to be independent. – Gautham M Nov 28 '21 at 13:37

1 Answers1

-1

You can always make the attributes public if you do not want to type multiple getters and setters. Additionally, in Java, the code should be:

public interface InterfaceCombination implements Interface1, Interface2 {

Not

public interface InterfaceCombination extends Interface1, Interface2 {
Holly
  • 16
  • 2
  • 1
    Thanks for your answer. If I make attributes public, users should access `name` by `ClassCombination.class1.name` not the expected `ClassCombination.name`. Additionally, in Java, `interface` extends `interface`, `class` implements `interface`, `class` extends `class`. – IcePear-Jzx Nov 27 '21 at 06:39
  • Please note that making the attributes `public` is not a good solution to "I do not want to type multiple getters and setters", as most IDEs provide you an option to generate getters and setters automatically. Also, getters and setters do have a lot of [advantages](https://stackoverflow.com/a/1568230/7804477). (+ Interface can only **extends** other interfaces and not **implements** it) – Gautham M Nov 28 '21 at 13:50