0

I was learning how to use the interface thing in java. I have couple of questions.Firstly, i really like the Interface thing because it helps me to declare methods and i can use those methods in different classes with different functionality. That's the cool part. Ok, this idea of declaring a method in the interface and then using it in different classes in different ways..isn't this polymorphism? Also, i was thinking it would be really easy if i can declare variables in the interface and then use those variables in my other classes but the problem is , whenever i try to reinitialize the value of the variables, it gives me the error called variable declared as final(Can't be modified). Is there no way i can use the variables declared in the interface in my other classes?

I am still new in java and so if my questions sounds stupid, I am sorry! .Thanks

Missy Zewdie
  • 231
  • 2
  • 9
  • 17
  • You cant define variables in interface – Wand Maker Jul 19 '15 at 15:23
  • Fields (variables) in interfaces are by default `static` and `final`. So you cannot reassign them. – Codebender Jul 19 '15 at 15:23
  • You should read up some [tutorials about interfaces](https://docs.oracle.com/javase/tutorial/java/concepts/interface.html). Firstly, interfaces are a means to provide something similar to multi-inheritance since Java does not allow multi-inheritance. Therefore yes, interfaces rely heavily on polimorphism. Second: you can only define `static final` variables within interfaces. If you need to get/set certain variables, write getters and setters within the interface. – Turing85 Jul 19 '15 at 15:24
  • 3
    @WandMaker, you can of-course define them. – Codebender Jul 19 '15 at 15:24
  • @Codebender In java? If yes, then, that will be eye-opener for me – Wand Maker Jul 19 '15 at 15:25
  • Yes, *declaring methods* in the *interface* and *defining* them in a *class* is *polymorphism* (its called method *overriding*). Next, you can't override fields . By default an interface fields are `public static final` and only constants should be defined in an interface. Note : in java-8 you can have default methods in an interface. – TheLostMind Jul 19 '15 at 15:26
  • @WandMaker Yes. You can define variables within interfaces, but they are always implicitly `static final` – Turing85 Jul 19 '15 at 15:27
  • @TheLostMind:- So, in short i cant? But then i have these 3 or 4 classes and each of them use these same variables, i was wondering that instead of declaring them in each class, is there a way i can declare those variables once and use them in my classes? That would ease my work a bit. The classes that i am talking about already implements an interface . – Missy Zewdie Jul 19 '15 at 15:29
  • 1
    @MissyZewdie You can make one common abstract superclass of this classes which `implements` your interface, in which you define the commmon attributes and methods. – Turing85 Jul 19 '15 at 15:30
  • @Turing85:- But then those classes already implements from an interface because my lecturer wanted us to use interface. So, making a superclass wouldn't be of much help. What do you say? – Missy Zewdie Jul 19 '15 at 15:31
  • @MissyZewdie, interfaces are only meant to declare behavior. Not posses some state (Which means no variables). But only declare methods. If you want to posses state, it must be a class (probably abstract). – Codebender Jul 19 '15 at 15:31
  • @MissyZewdie - Then use an abstract class which implements your interface.. declare your fields with default values there.. Then all your *concrete* classes extend this abstract class.. They will have default values of your fields.. you can change their values. – TheLostMind Jul 19 '15 at 15:32
  • @MissyZewdie interfaces and inheritance are orthogonal. That is the whole reason behind interfaces: your classes already `extends` a class, but you want to add something more. So you write an interface. In this way, your classes can `extends` the superclass and `implements` the interface. – Turing85 Jul 19 '15 at 15:33
  • @TheLostMind:- Ok, sounds awesome! Let me try! If it works then you will be called "TheGreatMind" in my book, not the Lost mind – Missy Zewdie Jul 19 '15 at 15:33
  • @MissyZewdie - Lol... OK.. Try it.. Get back to us if you have any issues.. Happy Coding :).. – TheLostMind Jul 19 '15 at 15:34
  • @Turing85 - I wouldn't use the term *orthogonal*. What if you extend an abstract class which has only method declarations? :) – TheLostMind Jul 19 '15 at 15:37
  • @TheLostMind Uh... then you inherit the methods and are forced to be either `abstract` or implement all `abstract` methods? Inheritance and Interfaces do not interfer. The general mechanism is not the same. Therefore they are orthogonal. – Turing85 Jul 19 '15 at 15:44
  • @Turing85 - an `abstract` method has to be implemented .. just like a method declaration in a an interface has to be implemented.. – TheLostMind Jul 19 '15 at 15:47
  • @TheLostMind Yes, I know. But the internal handling within the JVM and the bytecode is quite different. Otherwise, it would not be possible to `implements` multiple interfaces (which is the reason why Interfaces are slightly slower than abstract classes). Based on this knowledge, I wrote that those mechanism are orthogonal. – Turing85 Jul 19 '15 at 15:50
  • @TheLostMind:- One question. Say, the classes i have uses a common method. So, i should declare that method in my interface and make the abstract class implements the interface. Then, i write the body of the method in my abstract class. After that i can extend the abstract class in my other classes and then call that method. Is that a good idea? Ok i am still a beginner and forgive me if i sound stupid. – Missy Zewdie Jul 19 '15 at 15:54
  • @MissyZewdie yes. This is the general approach to avoid code repetition. Also, if you have to change the method for whatever reason, you have to change only **one** method, reducing the chance of inconsitency bugs. – Turing85 Jul 19 '15 at 15:56
  • 1
    @MissyZewdie - Yep.. That's the right way to go... Prevents code repetition... Also, code to the interface.. something like ==> `YourInterface i = new YourConcreteClass();`. We all start at level 0 .. So, please don't apologize.:) – TheLostMind Jul 19 '15 at 15:58
  • @Turing85 - Yep.. the bytecode is different for interfaces and abstract classes.. I think we are both trying to say the same thing in our own ways :) – TheLostMind Jul 19 '15 at 16:00
  • 1
    @TheLostMind most probably, yes ;) – Turing85 Jul 19 '15 at 16:01

0 Answers0