This question already has answers here:
Should methods in a Java interface be declared with or without a public access modifier?
(12 answers)
Closed 8 years ago.
I have maybe a simple question.
Here two code-snippets to show what I mean:
Example 1:
public interface SomeInterface{
public void someMethod(...);
}
Example 2:
public interface AnotherInterface{
void anotherMethod(...);
}
So, Example 1 is completely clear to me but Example 2 isnt.
In fact, is there any difference between those two examples expect the public-modifier?
On one hand I found that methods from Interfaces are implicitly public but on the other hand I have found that methods declared in an Interface are "package-public" (I dont now if thats the correct description) - saying these are visible to all classes in the same package as the Interface.
For now I am completely confused.. So could someone please explain me whats right?
Thanks anyways.
It's redundant to declare it public. In particular JLS 9.4 states:
Every method declaration in the body of an interface is implicitly public (§6.6).
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.
All interface methods are public abstract and all interface fields are public static final.
So there is no difference in above examples.
All methods in an interface are public and visible to implementing classes everywhere. But if the interface itself is package local (has no modifier - the default), then those methods are visible only to classes/interfaces within the same package. But the method would still have to be public in the implementing class
In the code you have above, there is no difference. But if it had been:
interface AnotherInterface{ // Note no modifier - default modifier applied
void anotherMethod(...);
}
In this case, the interface is visible only inside the same package.
NOTE: It's the interface itself that can be package-private, not the methods in it. You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. If a class implements that interface, the methods it defines must be public. The key thing here is that it's the interface type that isn't visible outside the package, not the methods.
Related
This question already has answers here:
Are all methods of interface abstract?
(7 answers)
Why Functional Interfaces in Java 8 have one Abstract Method?
(4 answers)
what is a abstract method on a interface in java [duplicate]
(4 answers)
When we add two abstract methods in an interface and implement just one method then why can't we implement the other method using lambda?
(1 answer)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Is the purpose of abstract keyword in a FunctionalInterface to distinguish the 'lambda' method from other abstract methods?
Since methods that have no body in an interface are abstract, and Functional Interfaces that have only one method do not have the abstract keyword, I was wondering whether the real purpose of the abstract keyword is to mark or distinguish which among the 'abstract' methods in a Functional Interface will serve as the 'carrier' of the lambda expression.
Is my understanding correct?
This method does not have the abstract keyword but is a functional interface:
public interface AllTrades {
public boolean checkSalary(ATrade t);
}
now suppose the interface above had 2 methods, they are both abstract,
but the interface is no longer a FunctionalInterface
public interface AllTrades {
public boolean checkSalary(ATrade t);
public boolean checkSalary2(ATrade t);
}
Now if we mark which method is abstract, it is a Functional Interface again:
public interface AllTrades {
abstract public boolean checkSalary(ATrade t);
public boolean checkSalary2(ATrade t);
}
Is my understanding correct?
Although you can use abstract in an interface, it has no extra meaning, as all methods in an interface (except default, private or static methods) are by definition abstract. It has no extra meaning in a functional interface, and if you annotate your interface with #FunctionalInterface, both of your examples with two methods will fail to compile as they are equivalent.
To be complete, your first example does have an abstract method, and is a potential function interface. Your second and third example both have two abstract methods, and cannot be used as a function interface.
See also the Java Language Specification 17, section 9.4:
An interface method lacking a private, default, or static
modifier is implicitly abstract. Its body is represented by a
semicolon, not a block. It is permitted, but discouraged as a matter
of style, to redundantly specify the abstract modifier for such a
method declaration.
(emphasis mine)
and section 9.8:
A functional interface is an interface that is not declared sealed
and has just one abstract method (aside from the methods of
Object), and thus represents a single function contract.
All methods in interfaces are public and abstract by default, and the inclusion of either of those modifiers does not change the behavior of the code in any way. You can verify this by compiling the second and third code samples with the #FunctionalInterface annotation, which would result in a compile time error.
Edit:
From the Java Language Specification
9.8. Functional Interfaces
9.4.3. Interface Method Body
9.6.4.9. #FunctionalInterface
This question already has answers here:
Java abstract interface
(9 answers)
Should methods in a Java interface be declared with or without a public access modifier?
(12 answers)
Closed 2 years ago.
interface Airplane {
String fuelOption = "kerosene";
public abstract String getFuelOption();
}
Is it necessary to specify public or public abstract in the line with the getFuelOption() method?
Thank you!
The public access modifier is not needed because
Every method declaration in the body of an interface is implicitly
public (§6.6). It is permitted, but discouraged as a matter of style,
to redundantly specify the public modifier for a method declaration in
an interface. (Section 9.4)
The abstract access modifier is not needed because
A default method is a method that is declared in an interface with the
default modifier; its body is always represented by a block.
And...
An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block.
Given that default methods have a body, and those that don't are inherently abstract, and every method declaration on an interface is inherently public, you don't need to specify either keyword.
Please refer below JLS :
https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#:~:text=Every%20method%20declaration%20in%20the,method%20declaration%20in%20an%20interface.&text=Default%20methods%20are%20distinct%20from,which%20are%20declared%20in%20classes.
No, All interface methods are public and abstract by default.
relevant text from the Java language spec, Section 9.4
A method in the body of an interface may be declared public or private .If no access modifier is given, the method is implicitly
public.
An interface method lacking a private, default, or static modifier is
implicitly abstract. Its body is represented by a semicolon, not a
block.
This question already has answers here:
how to restrict protected method access to only subclasses
(2 answers)
Closed 8 years ago.
How could I create a method in a given class that can only be called internally by it, and its subclasses.
For example, the class Foo:
public class Foo{
public Foo(){
}
???? void bar(){
}
}
and the class Baz, extending Foo:
public class Baz extends Foo{
public Baz(){
this.bar(); //valid
}
}
but also, some other random class, Qux:
public class Qux{
private Baz baz = new Baz();
public Qux(){
baz.bar(); //invalid
}
}
note that any of these classes could be in any package, so a simple 'protected' wont work.
if this isn't possible with a keyword or something similar, i'm open to suggestions on how to achieve similar behavior.
There is no visibility level you can use. The levels are strictly nested in Java, and a level that would give access to subclasses but not the package would break that.
In Java 1.0 there was an access level "private protected" that did just this, and it was removed from later versions because it complicated the language and wasn't very useful: A subclass may be created by someone you don't trust, while code in the same package is controlled by you. Therefore it's more important to protect yourself from subclasses.
This is not really possible with an access modifier. As you have only two choices between protected modifier (subclasses + classes in the same package) and default modifier (classes in the same package).
The only way you can achieve this by removing all non-child classes to other packages with protected/default access specifier.
public is the only keyword that allow to call a method from non-extending class in a different package. So if you still want to hide the method from pulic view and put your class anywhere, you will have to invoke the method via reflection (which is dirty).
This question already has answers here:
Protected in Interfaces
(15 answers)
Closed 9 years ago.
In interfaces why method access specifier is only public why not protected?
interface IPractice {
void test(); // it will be public
protected void test2(); // why this is not allowed
}
Can any one explain me this.
The whole point of an interface is that it exposes methods to the outside world so implementation details can be hidden.
What happens inside the interface should not be known to the outside world.
Because an Interface is public by nature, if you declare an Interface is because you want to make sure that everyone implements the same methods and such methods are PUBLIC.
Think of an interface as the controls of a car (Steering Wheel, Brake, Clutch...) no matter what, that is ALWAYS visible.
If you want several classes to implement the same method but make it protected, you may want to consider an abstract class instead.
An Interface is used to access the functionality of the class which is implementing it so you can assign object of class to the Interface reference. And you can call methods from that reference. So only public functionality can be accessed.
Do all methods in an Interface has by default Public visibility mode?
All methods in an interface default to public.
See Java Language Specification 6.6.1 which states
All members of interfaces are
implicitly public.
All interface methods ARE public abstract, all interface fields are public static final...
see here.
Just to add to other answers here: all methods are public, however, if the interface itself is package-local then effectively all methods are also package-local.
You can therefore mix public and package-local methods, by making a package-local interface extend a public one.
public interface P{
void iAmPublic();
}
interface L extends P{
void iAmPackageLocal();
}
Here L effectively has one public and one package-local method. Clients from outside the package will only see iAmPublic(), whereas ones from inside the package will see both methods.
In the same way you can nest interfaces inside other classes to achieve even tighter method visibility.
Yes, all methods of an interface are public, and can't have any other access modifier (i.e. the default public access modifier is the only valid access modifier)
Yes, all methods in an interface are implicitly public and abstract.
Check Java language specification chapter 9.4