Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes / member variables in interfaces?

Tags:

java

interface

I wish to know is there any way in which I can make it compulsory for the implementer class to declare the objects handles/primitives as they do with methods. for e.g.:

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 how can we compel Tile class to declare height and width attributes using the interface? For some reason I wish to do it with interface only!

I initially thought of using it with inheritance. But thing is I have to deal with 3 classes.!

  1. Rectangle
  2. Tile
  3. JLabel.!

 

 class Tile extends JLabel implements Rectangle {} 

would work.!

but

class Tile extends JLabel extends Rectangle {} 

woud not.!

like image 903
Shrey Avatar asked Sep 05 '11 17:09

Shrey


People also ask

Can an interface have member variables?

Yes, Interfaces CAN contain member variables. But these variables have to be implicitly (without any keyword definition) final, public and static. This means that within interfaces, one can only declare constants. You cannot declare instance variables using interfaces.

CAN interfaces have attributes?

Interface attributes are by default public , static and final. An interface cannot contain a constructor (as it cannot be used to create objects)

What kind of variables do interfaces have?

Interface variables are static because java interfaces cannot be instantiated on their own. The value of the variable must be assigned in a static context in which no instance exists.

CAN interface have attributes C#?

No. Whenever implementing an interface or overriding members in a derived class, you need to re-declare the attributes.


1 Answers

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

Basically an interface says here are all of the methods that a class that implements it must support. It probably would have been better if the creators of Java had not allowed constants in interfaces, but too late to get rid of that now (and there are some cases where constants are sensible in interfaces).

Because you are just specifying what methods have to be implemented there is no idea of state (no instance variables). If you want to require that every class has a certain variable you need to use an abstract class.

Finally, you should, generally speaking, not use public variables, so the idea of putting variables into an interface is a bad idea to begin with.

Short answer - you can't 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  { } 
like image 115
TofuBeer Avatar answered Sep 28 '22 21:09

TofuBeer