Attributes / member variables in interfaces?

I want to know if there is any way by which I can make it mandatory for the implementing class to declare object descriptors / primitives, as is done with methods. eg,

public interface Rectangle { int height = 0; int width = 0; public int getHeight(); public int getWidth(); public void setHeight(int height); public void setWidth(int width); } public class Tile implements Rectangle{ @Override public int getHeight() { return 0; } @Override public int getWidth() { return 0; } @Override public void setHeight(int height) { } @Override public void setWidth(int width) { } } 

In the above method, can we force the Tile class to declare height and width attributes using the interface? For some reason I want to do this only with an interface!

At first I thought about using it with inheritance. But the fact is that I have to deal with 3 classes.!

  • Rectangle
  • Tile
  • JLabel.

  class Tile extends JLabel implements Rectangle {} 

will work.!

but

 class Tile extends JLabel extends Rectangle {} 

woud not.!

+43
java interface
Sep 05 2018-11-11T00:
source share
7 answers

The interface point is to specify the public API. The interface has no state. Any variables that you create are really constants (so be careful when creating mutable objects in interfaces).

Basically, the interface says that these are all methods that the class that implements it must support. It would probably be better if the creators of Java did not allow constants in interfaces, but too late to get rid of it now (and there are some cases where constants are reasonable in interfaces).

Since you simply indicate which methods should be implemented, there is no concept of state (no instance variables). If you want each class to have a specific variable, you need to use an abstract class.

Finally, you should generally not use public variables, so the idea of ​​entering variables into an interface is a bad idea to start with.

The short answer is that you cannot do what you want, because it is "wrong" in Java.

Edit:

 class Tile implements Rectangle { private int height; private int width; @Override public int getHeight() { return height; } @Override public int getWidth() { return width; } @Override public void setHeight(int h) { height = h; } @Override public void setWidth(int w) { width = w; } } 

an alternative version would be:

 abstract class AbstractRectangle implements Rectangle { private int height; private int width; @Override public int getHeight() { return height; } @Override public int getWidth() { return width; } @Override public void setHeight(int h) { height = h; } @Override public void setWidth(int w) { width = w; } } class Tile extends AbstractRectangle { } 
+49
Sep 05 '11 at 18:02
source share

You can only do this with an abstract class, not an interface.

Declare a Rectangle as an abstract class instead of an interface and declare methods that should be implemented by the subclass as public abstract . Then the Tile class extends the Rectangle class and must implement the abstract methods from Rectangle .

+7
05 Sep 2018-11-17T00:
source share

Interfaces cannot require the definition of instance variables - only methods.

( Variables can be defined in interfaces , but they do not behave as you would expect: they are treated as final static .)

Happy coding.

+6
Sep 05 2018-11-17T00:
source share

In Java 8, default methods for interfaces were introduced with which you can use methods. According to the OOP interfaces, it should act as a contract between the two systems / parties.

But still I found a way to achieve saving properties in the interface. I admit this is a pretty ugly implementation.

  import java.util.Map; import java.util.WeakHashMap; interface Rectangle { class Storage { private static final Map<Rectangle, Integer> heightMap = new WeakHashMap<>(); private static final Map<Rectangle, Integer> widthMap = new WeakHashMap<>(); } default public int getHeight() { return Storage.heightMap.get(this); } default public int getWidth() { return Storage.widthMap.get(this); } default public void setHeight(int height) { Storage.heightMap.put(this, height); } default public void setWidth(int width) { Storage.widthMap.put(this, width); } } 

This interface is ugly. To store a simple property, he needed two hashmaps, and each hash file by default creates 16 default entries. In addition, when the real object is dereferenced, the JVM also needs to remove this weak link.

+4
Sep 18 '14 at 8:29
source share

In Java you cannot. The interface is associated with methods and signature, it is not related to the internal state of the object - this is an implementation issue. And that also makes sense - I mean, simply because certain attributes exist, this does not mean that they should be used by the executive class. getHeight may actually point to a width variable (assuming the implementer is a sadist).

(As a note, this does not apply to all languages, ActionScript allows the declaration of pseudo attributes, and I believe that C # also)

+2
Sep 05 2018-11-17T00:
source share

Fields in interfaces are implicitly public static final . (Methods are also implicitly public, so you can opt out of the public keyword.) Even if you use an abstract class instead of an interface, I strongly recommend that you make everything non-persistent ( public static final primitive or immutable object references) private . More generally, they "prefer composition for inheritance" - a Tile is-not-a Rectangle (of course, you can play game games with "is-a" and "has-a").

0
Sep 05 '11 at 17:58
source share

Something important was said by Tom:

if you use the has-a concept, you avoid this problem.

In fact, if instead of using extensions and tools you define two attributes, one of the types of rectangles, one of the JLabel types in your Tile class, then you can define a Rectangle as an interface or a class.

In addition, I would usually encourage the use of interfaces in connection with has-a, but I assume that this will be redundant in your situation. However, you are the only one who can decide on this issue (compromise flexibility / over-engineering).

0
12 sept. '14 at 14:22
source share



All Articles