Implementing the Interface containing varaibles

Instance variables declared in an Interface are treated as 'final' type by default. When you implement an interface containing Instance Variables in your class, you can't modify the values of Instance variables belonging to Interface  in your class.

Lets implement this on Eclipse IDE: 

(First lets print the implemented interface variables)

1. Create Interface 'Interface1' containing few instance variables as shown below:



2. Create class 'ClassOne' which implements the 'Interface1' and prints the instance variables of 'Interface1' in the 'ClassOne' class:



3. Create class 'InterfaceVaraiblesDemo' to create an object for accessing the 'pritntVariables( )' method of 'ClassOne' class as shown below:



4. Save and Run the 'InterfaceVaraiblesDemo' class
5. Observe that the output is displayed in the console as shown below:



Download this project:

Click here to download the project containing the 'Interface1', 'ClassOne' and 'InterfaceVariablesDemo' files used in this post (You can download the project and import into Eclipse IDE on your machine)

(Now lets find out what happens when we modify the Instance variable of Interface in 'ClassOne' class)

1.In 'ClassOne' class of above project, create another method 'printModifiedValues( )' and try to modify the values of instance variables of 'Interface1' as shown below:



2. Observe that the errors "Interface variables cant be modified" are displayed in the statements that modify the instance variables of Interface as shown below:




After looking at these errors, its very clear that Instance Variables of Interface can't be modified in the Class which implements the Interface as all the instance variables of Interface are by default 'final' though you have not specified any type while declaring them in Interface.