![]() ![]() If they add them as static methods, then programmers would regard them as utility methods, not as essential, core methods.ĭefault methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. Where would these car manufacturers declare these new flight-related methods? If they add them to their original interfaces, then programmers who have implemented those interfaces would have to rewrite their implementations. What if those computer-controlled car manufacturers add new functionality, such as flight, to their cars? These manufacturers would need to specify new methods to enable other companies (such as electronic guidance instrument manufacturers) to adapt their software to flying cars. When working with multiple inheritance: If we. It is unfortunate that the "implicitly public" behaviour was ever allowed as it will now likely remain for backward compatibilty and lead to the confusion that the absence of the access modifier means 'public' in interfaces and 'package' elsewhere.Interfaces describes an example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. By using the Java 8 default method we can add a default implementation of that method which resolves the problem. I think that last sentence should have been removed from the JLS. ![]() Style, to redundantly specify the public modifier for a method It is permitted, but discouraged as a matter of If no access modifier is given, the method is implicitly public. "A method in the body of an interface may be declared public or "Package access is not allowed in an interface.") we would get rid of the apparent ambiguity that having the option to leave out 'public' introduces. If you simply made it a compile error with a clear message (e.g. Why? Because the absence of a modifier means 'package' access everywhere else, and having this as a special case is what leads to the confusion. ![]() I think instead Java should enforce/require 'public' to be specified. The fact is that now with Java 9 and beyond there ARE other options. I disagree with the popular answer, that having public implies that there are other options and so it shouldn't be there. So in conclusion although using public abstract before methods and public static final before constants of an interface is valid but since there is no other options it is considered redundant and not used. Therefore static final also is a must for interface constants. Considering this fact, since no method can be implemented in an interface therefore all the methods in interface must be abstract.įourth, Interface can only include constant as its data members which means they must be final and of course final constants are declared as static to keep only one instance of them. If there is logical reason to insert implemented methods or instance variables in an interface then it must be a superclass in an inheritance hierarchy and not an interface. Third, an interface does not have any data implementation including the instance variables and methods. So for the access modifier we are only left with public choice. And since an interface might be implemented (realized) by different unrelated classes which are not in the same package therefore protected access modifier is not valid as well. Second, Although one can initiate objects of an interface type but an interface is realized by the classes which implement it and not inherited. Therefore it is not possible to specify the access modifier as private since it cannot be accessed by other classes to be overridden. Despite the fact that this question has been asked long time ago but I feel a comprehensive description would clarify why there is no need to use public abstract before methods and public static final before constants of an interface.įirst of all Interfaces are used to specify common methods for a set of unrelated classes for which every class will have a unique implementation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |