Reputation: 51
I understand that variables are static since interface cannot be instantiated and interface variables is not specific to object since it is static.
We know that static vaiables or methods cannot be instantiated by object.we have to call them by class name.
I have been digging into interfaces to understand it better.
When class extends interface, variabLes in interface accessible without using interface name. How is it working?
Can anyone explain it further for my understanding?
Upvotes: 1
Views: 1707
Reputation: 3048
I'd like to present a slightly different view than those of other answers, hopefully a bit more intuitive.
Interfaces can generally be thought of as abstract classes with only virtual (abstract) methods. The difference is that in Java you can implement multiple interfaces but you can't inherit from multiple classes.
If you have a class that defines a static final field, all its subclasses have access to it just like they have access to other (non-private) field. When you think of interfaces as of classes, the answer to your question becomes obvious - it's part of the superclass (interface) and it should be inherited.
Note that there will be problems if you define the same name in two interfaces - that's one of the reasons Java doesn't have multiple inheritance in the first place.
Upvotes: 0
Reputation: 726509
We have to [qualify access to static fields] by class name.
This is true only when you are accessing these fields from outside the class. Inside methods of the class itself, however, such dereference is not necessary:
class Test {
public static final String NAME = "Hello";
public void sayName() {
System.out.println(NAME);
}
}
The above code compiles, even though NAME
is a static field.
When class extends interface, variabLes in interface accessible without using interface name. How is it working?
The same logic applies to inherited static fields. As long as the class is allowed to access a field, qualifying this access with the class or interface name from inside the inheriting class is optional.
Upvotes: 1
Reputation: 1074198
This is covered by §6.5.6.1 of the JLS:
If an expression name consists of a single Identifier, then there must be exactly one declaration denoting either a local variable, parameter, or field visible (§6.4.1) at the point at which the Identifier occurs.
Basically what it says is that if you give an unqualified identifier, then the compiler will match that up against a local variable, a parameter to the current method, or a field (instance or static) and use it; if there are no matches, or there are multiple matches, the code won't compile. So if it compiles, it means that the reference is unambiguous. (Which doesn't mean it's necessarily good practice; if you qualify it with the interface name, it will be clearer to other programmers, and of course won't have the problem of possible collision with other identifiers causing compilation errors.)
Upvotes: 0
Reputation: 3353
You don't have to access static variables using class name, it is just good practice.
static variables are accessible via instance reference too, the only counter-intuitive and unexpected situation arises when the instance reference is null
.
Re: Class implementing Interface inherits the static variables defined in the Interface
Upvotes: 0
Reputation: 36644
Classes which implement an interface can access the interface member fields because they are public by default, and implementing means that the class has a is-a
relationship with the interface.
See Is there any relation between the class that implements interface and that interface?
Upvotes: 0