Consider the followoing code
interface MyInterface{
void method(String s);// if we write static modifier we have compile error
}
class MyClass implements MyInterface{
public static void main(String[] args){
myMethod(new Object());//Compile error
}
static void method(String s){...}// compile error
static void myMethod(Number n){...}
}
method()
with static modifier? Consider the following code
Object someObj; ... Number n= (Number) someObj;
What compiler is doing when we cast to Number
in this case?
Why we cant define static method in interface?
All the methods of an interface are by default public abstract
. Using static
modifier doesn't make sense. Because the invocation of static methods aren't polymorphic. In the sense, you can't override them. You invoke the static methods on the class name only. Well, you can invoke them on some reference too, but that will be ultimately resolved on the basis of the declared type of the reference. Now, since the method is by default abstract, it doesn't make sense to invoke it. It doesn't have any body to do any task.
Why we cant implements method() with static modifier?
When you try to add static
modifier to the overridden method, it is not considered as being overriding. So, your class essentially have two different methods, with same name, same parameters, and same return type. That is of course not allowed inside a class.
Note that, you have to explicitly add public
modifier to the overriding method in the class, else your code won't compile. The reason being, you can't decrease the visibility of the overridden method in the subclass.
When we are calling myMethod with reference to Object we have compile error. As i understood, compiler doesnt cast automatically, isnt it?
Jave doesn't do automatic narrowing conversion. You need to add cast explicitly. But even if it allowed, how you expect your code to behave, because you are trying to refer a super class object with a subclass reference? You can of course make your code to compile by adding a cast while invoking the method:
myMethod((Number)new Object()); // This will compile, but fail at runtime
The above invocation will result in a ClassCastException
at runtime.
However, if you have an Object
reference refering to an object of any subclass of Number
, you can add an explicit cast, that will be type safe:
Object obj = new Integer(5);
Number num = (Number)obj; // This is fine. Because `obj` is referring to an `Integer`.
And finally, the signature of your main
method is not correct. You are missing the public
modifier.
Why we cant define static method in interface?
Interfaces are designed to work with polymorphism, basically. Polymorphism How would you know which implementation to call when calling a static method on an interface?
// Should that invoke MyClass.method or MyOtherImplementation.method?
MyInterface.method("foo");
Next:
Why we cant implements method() with static modifier?
The idea is that the method is called on some object which implements the interface - which makes it an instance method.
When we are calling myMethod with reference to Object we have compile error. As i understood, compiler doesnt cast automatically, isnt it?
No, the compiler doesn't cast automatically. There's no implicit conversion from Object
to Number
, so you can't call a method with a parameter of type Number
with an argument of type Object
.
What compiler is doing when we cast to Number in this case?
It's validating that the value of someObj
is either null or a reference to an instance of Number
or a subclass.
until JDK7:
because static methods are bound to the class. you normaly invoke them like this:
MyClass.method("");
you can not override them.
see 1 and all interface method are public abstract
you can not change that!
no the compiler does not cast automatically
he tries to cast and fails
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.