15

Sorry I really didn't know how to title the question, here's the problem...

I have an interface and multiple classes that implement the interface. The implementation of some of the methods in the interface are exactly the same in every implementing class. I feel like there should be a way to simplify this so I don't have to write the same code every time. Example:

public interface Foo {
    String getName();
}

public class FooImpl1 implements Foo {
    private String name = "foo name1";

    public String getName() {
        return name;
    }
}

public class FooImpl2 implements Foo {
    private String name = "foo name2";

    public String getName() {
        return name;
    }
}

So to break down..

  1. is there a way to put the code for getName in one place and each class has it's own name variable?

  2. is there a way to make getName static so I don't have to create a new instance

Have better ideas?

1
  • 8
    You could define a abstract class that is the parent of those two and that contains the name field and the get name method. Child classes will not need to implement it. Commented Aug 31, 2014 at 21:16

3 Answers 3

12

Use an abstract class and initialize the field in the constructor:

public abstract class Foo {

 protected String name;

 String getName() {
  return name;
 }
}

public class FooImpl1 extends Foo {

 public FooImpl1 () {
  name = "foo name1";
 }
}

public class FooImpl2 extends Foo {

 public FooImpl1 () {
  name = "foo name2";
 }
}

JB Nizlet pointed out that it would be cleaner to do something like this:

public abstract class Foo {

protected String name;

public Foo(String newName) {
    name = newName;
}

 String getName() {
  return name;
 }
}

And then call super("foo name 1") in the subclasses.

As @Peter Lawrey said, if you already have an interface or want one for some other reason:

public interface IFoo {
 String getName();
}

public abstract class Foo implements IFoo {

 protected String name;

 String getName() {
  return name;
 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 If you need an interface, the abstract class can implement the interface.
Would be cleaner to take the name in the superclass constructor and to call super(name)
1

You could take a very simple approach:

public interface Foo {

    String getName();
}

public class Named implements Foo {

    String name;

    public String getName() {
        return name;
    }
}

public class FooImpl1 extends Named {
    {name = "foo name1";}
}

public class FooImpl2 extends Named {
    {name = "foo name2";}
}

2 Comments

@AnubianNoob - Assuming you're talking about the {name = ...} parts, those are initialization blocks. On another note, I always find questions/comments asking "is this valid" pointless because you can usually just run the code through a compiler/interpreter as easily as ask if it's valid, and you get an immediate answer.
Well I know it's valid. What I'm asking is what it's called (thanks) and maybe even a link (thanks a lot).
0

It doesn't really work for getters like in your example, but since the question is more generic I think it's worth mentioning that with Java 8 you can define default methods in interfaces. So if you had something a bit more complex like:

public interface ComplexFoo {
    String getFirstName();
    String getLastName();

    String getFullName();
}

You do not have to repeat the getFullName implementation in every subclass, but can specify it in the interface:

public interface ComplexFoo {
    String getFirstName();
    String getLastName();

    default String getFullName() {
        return getFirstName() + " " + getLastName();
    }
}

The getters though still need repeating, since they access members that are not yet available in the interface:

public class FooImpl1 implements ComplexFoo {
    private String firstName = "foo";
    private String lastName = "name1;

    private String getFirstName() { return firstName; }
    private String getLastName() { return lastName; }
}

public class FooImpl2 implements ComplexFoo {
    private String firstName = "foo";
    private String lastName = "name2;

    private String getFirstName() { return firstName; }
    private String getLastName() { return lastName; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.