+ 7
[Answered: Java v.8 difference] What's wrong (and what's right) with this question about interfaces?
"Interfaces may contain only abstract methods". And the answer is "False". Is it my English? Interfaces surely can only contain abstract (in java, virtual in c#...) methods (must contain only abstract methods, can not contain not abstract methods). Does "may" plays a big role here? Does "may" implies that interfaces also may (explicitly/implicitly) contain NOT abstract methods? Is it just bad wording (neded to be reworded)? (Isn't it another bad question then?)
16 Respuestas
+ 6
There is no ambiguity here.
Since java 8 release, interfaces may contain realization of the default methods.
+ 5
@Andrew don't try to cover a lot of languages. Be perfect in 1 or 2, learn the rest if necessary)
+ 5
@Alexey (Now) We are stating that "may contain only abstract methods" is not the same with "may not contain any not abstract methods" (the last one statement is falsy as we've found out, thanks to you). "May contain abstract methods" is true even if there is possibility for interfaces to contain not abstract methods. ('Cause there is still may be interfaces with only abstract methods).
+ 5
@Andrew
Already read posts with pay attention)
+ 5
Thank you all. I feel that I have to stop/quit/drop being addicted to sololearn and read good books. Anyway without noting (noticing?) version of java the question is incorrect. And even if there would be a note about version the wording is bad 'cause it allows different interpretations.
+ 4
Added: read your posts inattentively.
Question is clear.
@Andrew Kharchenko, @Rrestoring faith
Why only abstract methods?
The body of the default methods contains a complete implementation.
For example, for some classes implementing interface some methods are not needed. You would have to implement these methods in each class that do nothing. Now you can implement this methods as default in interface.
+ 3
@Alexey Hm didn't know about that, thanks for the info.
Though, I'm still confused on the word 'may'. They still may contain only abstract methods, regardless of default methods. Just don't add default methods.
+ 3
@Alexey Yep, I know. More of that: I feel disadvantages of my way very strong. :-/ But...
Can
Not
Resist
my own curiousity. :-))
And I'm not able to (finally) choose between Java and PHP (+JS then... but in Java in web JS would be usefull too) really. :-( (Luckily I was able to sort out C# and Python... but sometimes I feel pity of C# [Xamarin, "Windows Mobile" and Unity are of some interest for me] ) And I often remember how I liked C++ when I was studying it. :) Ready to dive deep in any domain if I'll be close to get a decent job in it...
+ 2
That question is horribly worded. Even if interfaces could contain methods that aren't abstract, they 'may' contain only abstract methods.
All methods of an interface are implicitly abstract anyway. Meaning it ONLY has abstract methods.
I'd say report the question.
Unless I'm missing something here... xD
Which I think I am.
+ 2
@Rrestoring faith and @all Let's report it then! I've gone to my challenges history... :)
+ 2
@Rrestoring faith Reported that question. I'll wait for some other suggestions to see if there something else to add here... but I think your answer is good enough to have a "best answer" mark... just need to check that it's not because of similar (to my own) opinion. ;-) (Checked that your profile states that you are from English speaking country... partially. :-))
+ 2
@Alexey Oh! Wow. Thank you! 8th version bring a lot of differences... starting from lambdas for example and a lot more. :-/ It's just contrary to what Sololearns course teached us! So I think this question still shouldn't be here or course must be updated... I've rechecked if there is something different from what I remember (always afraid of messing things up because trying to learn too many languages simultaneously [or in parallel] :-)) Update: Sorry, Rrestoring faith, even if we are at the same point about that question I feel that I _have_ to mark Alexeys _answer_ as best because it contains that very important information!
+ 2
In two parts as answer became too long
Part 1:
Since java 1,
interfaces can have only fields (static final implicitly) OR
interface can have only have methods (public and abstract implicitly) OR
interface can have
1. constants (fields) implicitly public, static and final. no other modifier allowed. Annotations can be applied
2. methods, implicitly abstract so obviously cannot be final. Are also public implicitly. No other modifiers allowed including implementation characteristics like native, synchronized or strictfp. Annotations allowed.
3. nested classes and interfaces - nested interfaces are static by conventions. nested classes are implicitly public. Rest details I am not sure!
So interfaces can have 1 or 2 or 3 or their combination.
+ 2
Part 2:
Since java 8,
interface can also have
4. static methods - implicitly public and final. But they do not belong to interfaces. acts like utility methods.
a. can access interface fields(1)
b. can have implementation.
c. can be accessed only by other interface methods(static or nonstatic), nested interfaces and classes and extending interfaces.
d. not accessible to implementing classes.
5. nonstatic interface methods can have implementation and can be marked default using the "default" keyword like
public default void Func()
{
// implementation details
}
the implementing class need not override this method to provide it's own implementation.
However if the implementing class is implementing two or more interface with each having the same signature of default interface method, then the implementing class MUST override the default implementation.
So since Java 8, an interface can have 1 or 2 or 3 or 4 or 5 or their various combinations.
when an interface extended another interface then it can inherit the default implementation of the interface method, remove the default implementation forcing implementing class to override interface method by providing it's own implementation or override the default implementation of the interface method with it's own version. In this case if the implementing class choose not to override the default implementation, then the default implementation provided by the last extended interface will be used as default implementation for the implementing class.
Also remember implementing interfaces is NOT an inheritance relationship.
+ 1
@Rrestoring faith Right you are! Even with that new info Interfaces STILL _MAY_ contain only abstract methods! :-)))
+ 1
the answer to this can be as simple as 'No the interfaces may not contain only abstract methods since they may also have concrete methods inside em (added feature in java 8 I guess) or the static final variables'... I saw an example in java the complete reference in which the interfaces had only final static variables