Reputation: 1552
If i have a interface car that is to be implemented on various classes called BMW , AUDI etc . Now i want all of them to have a string instance color in which their colors should be stored . As they all can have a different colors I can not define a variable in interface for it as obligatorily final thus its value would not change as per the need of classes . so is there any way by which i can ensure that the color is used in every class .
Also please let me know that why are the variables in the interface always final ie what would be the problem if they were not final variables . I have already reffed to following questions but none was satisfactory Why are interface variables static and final by default?
and Few others too.
Upvotes: 1
Views: 694
Reputation: 1552
I Searched the ans and the satisfactory ans i got was that the Instance members get memory when the constructor is called which is not possible in case of interfaces thus they need to be static so that they get the memory.
and as far as the reason for being final is considered its to avoid the data ambiguity . lets consider and scenario that when two classes will implement an interface and access a static data member that is static the change done from one class would effect the values of the other class also thus resulting in data ambiguity so they are made final.
Upvotes: 0
Reputation: 325
Why do you want to use an interface?
it seems better to use an abstract class to hold the color data (use of get/set on interface doesn't force the presence of the field...)
public abstract class AbstractAuto {
private String color;
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public abstract void ignite();
...
}
public class Audi extends AbstractAuto {
Audi()
{
setColor("red");
}
@Override
public void ignite() {
// TODO Auto-generated method stub
}
}
Upvotes: 2
Reputation: 8975
Interface means a contract which needs to be followed by the the classes implementing it.
So define certain things in the contract. Here you want user to force to have color then define method in the interface which will force him to have color
void setColor(String color);
String getColor();
Why variables of interface are final?
You will get many references for this. But understand in simple words. Interface means force to do some things to the classes implementing it. So we have variable as final as we force that classes should use this value only and not any other value.
static final int WHEELS=4;
Upvotes: 0
Reputation: 83
No, you can't force usage of an instance variable via an interface. You can only force the implementators to have a 'getColor()'. If you want an instance variable, you can create a base class with an instance variable and let others extend your class.
Upvotes: 0
Reputation: 24334
Your interface would provide methods like:
void setColor(String color);
String getColor();
Then the subclasses can implement these methods
You don't store the variable itself in an interface, as that is an implementation detail
Upvotes: 2