10

My question is how can I write setter/getter methods and static fields in an interface and implement it in another class.

An example:

public interface MyInterface {
int number = 0;

public int setNumber(int num);{

 }
}

// Use it

public MyClass implements MyInterface{

 ...

public int setNumber(int num) {
   number = num;   // Error, Why?
 }
}

I get error on number = num but it get no error in the setName(...) method!

Sajad
  • 2,273
  • 11
  • 49
  • 92
  • 1
    Besides all the correct answers, setNumber method currently must return an int value or it will no compile. – Averroes Dec 14 '12 at 12:17
  • Possible duplicate of [Java Interface Usage Guidelines -- Are getters and setters in an interface bad?](http://stackoverflow.com/questions/1130294/java-interface-usage-guidelines-are-getters-and-setters-in-an-interface-bad) – demongolem Nov 10 '16 at 17:47
  • you can use abstract class for this purpose – Muhammad Azam Aug 31 '21 at 15:06

5 Answers5

26

You cannot define instance fields in interfaces (unless they are constant - static final - values, thanks to Jon), since they're part of the implementation only. Thus, only the getter and setter are in the interface, whereas the field comes up in the implementation.

And setNumber should return a void instead of int. For getting I suggest you to add int getNumber().

public interface MyInterface {
  void setNumber(int num); // public is implicit in interfaces
  int getNumber();         // obviously
}

public class MyClass implements MyInterface {
  private int number = 0;
  
  public void setNumber(int num) { this.number = num; }
  public int getNumber() { return this.number; }
}

As you can see, only setNumber is part of MyInterface. Consumers do not need to know about how the number is stored, therefore it is an implementation detail.

Besides, in Java you name classes and interfaces in PascalCase rather than camelCase.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • 12
    Or rather, the only fields you can declare in interfaces are constants. – Jon Skeet Dec 14 '12 at 12:14
  • 1
    You should think it twice to see if you really want to put a setter in an `interface`. Given you don't know the implementation, you are probably saying too much if you force them to have a setter - dummy or constant implementations wouldn't need them. – mgarciaisaia Dec 14 '12 at 12:46
  • I agree. However, the OP already had the "lonely" setter, with I think is less common than a getter only. That's why I added both. – Matthias Meid Dec 14 '12 at 12:50
9

When you define a value in an interface it is implicitly public static final i.e. it's immutable and not an instance field.

Interfaces are used for defining a contract, not behaviour so it doesn't make sense to insist that all implementations have this field.

BTW You can do this if you have an abstract class as well as or instead of an interface

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

Interface can not contain method body definition and field are public, final and static by default which normally use for constant declaration. It will be defined where you are going to implement this interface.

In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

ref

But abstract class can contain concrete method as well as abstract method.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
4

You cannot change the fields declared in an interface as they are by deafulat public static final.

You see final so they cannot be changed once initialized.

And in an interface you cannot give implementation of a method as you are doing in your code.

just put ; after the method signature and not {} (not even empty {}) :

public int setNumber(int num);
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
3

Fields in interface are by default public static final i.e., constants.remember you have already initialized number in your interface and trying to change its value in your implementing class which breaks the laws of final variables .

From JLS:

A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

Also methods in interface are by default public abstract. i dont understand your method signature in your interface. it should be something like below.

public void method();
PermGenError
  • 45,977
  • 8
  • 87
  • 106