I am new to this community. I have few questions in my mind taking Interface
1) Interface can have variables and internally makes it public static and final. If we talk design wise where exactly we may need these variables?
And also in case an interface I1 extends another interface I2 then if I2 has a variable
public static final int testValI2=12;
so design wise this variable is accessible to I1. When we will be this kind of situation? Inheritance is the property of classes not of Interface so what does it exactly means when an interface is exteding another interface?
2) if Class A implements Interface I1 and interace has a variable which by default will be static? So when this variable be loaded as this is static?
Love thy Duty, Reward is not thy concern
Joined: Jun 01, 2010
What you are calling interface variables are really just constants (static final). You generally use constants for things like, for example, Strings that you don't want to hard-code into the code. You extend an interface for the same reason you extend a class. The base interface has functions (or constants) that a set of interfaces have in common. Static final variables are initialized when the class that implements the interface is loaded for the first time.
Joined: Dec 17, 2009
Having an interface for constants is considered to be anti-pattern.