如果没有继承,是否可以从不同的 类 重载方法?许多消息来源声称没有。但我没有收到任何错误。为什么呢?

Is method overloading from different classes possible without inheritance? Many sources claim no. But I don't get any error. Why is that so?

这是一个示例程序

class findArea   //Class
{
    public void area(int side) *//method with one int argument*
    {
        System.out.println("The area of square is:"+side*side);
    }
    public void area(int length, int breadth)//method with 2 int arguments
    {
        System.out.println("The area of rectangle is:"+length*breadth);
    }
    public void area(float radius)//method with one float argument
    {
        System.out.println("The area of circle is:"+3.14*radius*radius);
    }
    
}
public class MethodLoad {
    public static void main(String args[])
    {
        findArea fa = new findArea(); //Object creation
        MethodLoad m =new MethodLoad();//Object creation
        fa.area(4);
        fa.area(7.0f);
        fa.area(5,6);
        m.area(2.0f,3.5f);
    }
    public void area(float base, float height )//method with 2 float arguments
    {
        System.out.println("The area of triangle is:"+0.5*base*height);
    }
    
}

这里findAreaclass和MethodLoadclass没有父子关系。但是可以在没有任何错误的情况下重载方法。对不对请大侠指点一下

重载 Java 中的方法总是在 class 中,而不是来自不同的 classes

你不会得到任何错误,因为你所做的是允许的

MethodLoad 中的 area 方法没有重载 findArea 中的 area 方法; findArea 中的 area 方法是彼此的重载,但它们不是 findArea.

中的 area 方法的重载

不相关 类 中的同名方法不是重载:它们只是同名方法。