¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita0
Java Shape Quiz
Can anyone elaborate on the following line of code from the "Java Shapes Project" (in More On Classes): Square a = new Square(x); Circle b = new Circle(y); Specifically, I'm not understanding the "Square a" and "Circle b" parts??? Is it just assigning "Square(int x)" to "a" ? So when it's called later in "a.area()". If so, couldn't someone write "Circle a = new Circle(x);" How would it know if "a.area()" was for the Circle or Square???
1 Respuesta
- 1
Square a = new Square(x);
it creates object of type Square and its constructor can use value of x
then this new Object is assigned to variable a of type Square
q2: no, if type of a is declared yet
q3: because a has only one type, it is not problem