1

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?

Interface Variables

Variables in Interface

and Few others too.

Community
  • 1
  • 1
OshoParth
  • 1,492
  • 2
  • 20
  • 44
  • Interface means contract that needs to follow who is implementing it. So it has abstract methods which we need to implement. And final variables because that the value should be used in the subclasses implementing the interface. And subclass should not change that value which is out of contract – Shoaib Chikate Sep 30 '14 at 10:19
  • You have gone through one of the best explanations but wonder how you are not satisfied. – Imran Sep 30 '14 at 10:21
  • is there any restriction on defining a separate variable for color for each of the implementation classes and set them though a setter. – vikeng21 Sep 30 '14 at 10:28
  • @Imaran because everywhere the functioning of the final keyword is describes but i want to know that what would be the issue if the variables in the interfaces were not final why cant we use general variables ? – OshoParth Sep 30 '14 at 10:28
  • 1
    Because then it would be implementation not interface, your design here worries me though. I think you want an abstract class with a colour in it, not an interface at all. – Tim B Sep 30 '14 at 10:37

5 Answers5

2

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

cowls
  • 24,013
  • 8
  • 48
  • 78
1

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

    }
}
Dan M
  • 315
  • 2
  • 14
  • yeah you are absolutly right. But here you force it in the constructor not in the abstract class. So this way the interface will force to use those functions and at the contructur you can do the same. So technikally both are nice example. – SüniÚr Oct 02 '14 at 14:05
0

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.

Praveen Ray
  • 83
  • 1
  • 8
0

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;
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • as quoted in your example we have a variable wheels that has value 4 but my concern in this that we know every vehicle would have wheels so we want to force classes to use a variable named wheels but at the same time we also know that different variables can have different values of wheels so what to do in this case. – OshoParth Sep 30 '14 at 10:40
  • @OshoParth: You only gave your answer. We know that every car has 4 wheels but subclasses may make car of 3 wheels. So force them that you can make the car but have wheel 4. And this variable is for Cars only. – Shoaib Chikate Sep 30 '14 at 10:42
0

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.

OshoParth
  • 1,492
  • 2
  • 20
  • 44