Можно ли использовать два метода с одинаковым именем и одним и тем же параметром в JavaJAVA

Программисты JAVA общаются здесь
Anonymous
Можно ли использовать два метода с одинаковым именем и одним и тем же параметром в Java

Сообщение Anonymous »

Код: Выделить всё

//using the concept of method overloading
public class practice10 {

// Method to calculate the area of a rectangle
public static double calculateArea(double length, double width) {
return length * width;
}

// Method to calculate the area of a square
public static double calculateArea(double side) {
return side * side;
}

// Method to calculate the area of a circle
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}

public static void main(String[] args) {

// Calculate and display the area of a rectangle
System.out.println("Area of rectangle (length=5, width=4): " + calculateArea(5.0, 4.0));

// Calculate and display the area of a square
System.out.println("Area of square (side=4): " + calculateArea(4.0));

// Calculate and display the area of a circle
System.out.println("Area of circle (radius=3): " + calculateArea(3.0));
}
}
Я взял этот код из ChatGPT, в нем не было никаких ошибок, но когда я выполнил это, возникла ошибка программы.

Подробнее здесь: https://stackoverflow.com/questions/783 ... ed-in-java

Вернуться в «JAVA»