界面:php vs java [关闭]

I notice something very different between java and php on interface, when you create the same methods in their interfaces.

PHP:

interface Visitor 
{
    public function visit(Visitable $Visitable);
    public function visit(Visitable2 $Visitable2); // this is wrong in php.
}

Java:

interface Visitor 
{
    public double visit(Visitable Visitable);
    public double visit(Visitable2 Visitable2); // this is ok in java
}

you can look at the check it out on this video at 3:35 for java.

how come java allows that? what does double do anything? and no double in php?

Java actually allows overloading: this means that you can have a method with the same name but with two different signature.

In the case of Java there are two methods with the same name visit in overloading:

  1. one accepts a param of type Visitable;
  2. the other Visitable2.

You cannot do that in PHP because PHP doens't support overloading.

Also please notice that double (the method return type) in that case does nothing. The return type alone cannot be used to achieve overloading as the compiler/interpreter has no way to know which implementation to bind in some circumstance.