0

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???

14th Sep 2021, 11:09 PM
Joel Mahaffey
Joel Mahaffey - avatar
1 Resposta
- 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
15th Sep 2021, 1:56 AM
zemiak