Reputation: 55
Help, I just started learning Java, and this online tutorial I am doing is asking me to create an instance that belongs to a class. This instance is supposed to be created as a "Rectangle" object first, then later as a "Circle" object. But Eclipse is asking me to rename the second "drawObject".
public class TestPolymorph {
public static void main(String[] args) {
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
Shape drawObject = new Circle(40);
drawObject.draw();
}
}
Upvotes: 3
Views: 1332
Reputation: 29
You can only assign an instance of a class to a reference of a class which either extends it or is of the same type. If not it wont allow you to do the same. In this case Circle and Rectangle should extend/implement Shape.
Upvotes: 0
Reputation: 396
You can not use same name for two variables within same scope. If Rectangle and Circle you extends Shape then you this will work.
public class TestPolymorph {
public static void main(String[] args) {
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
drawObject = new Circle(40);
drawObject.draw();
}
}
notice that I have remove Type declaration when I am assigning the same reference variable the second time i.e reference of New circle to the reference variable drawObject.
Upvotes: 1
Reputation: 2372
I go with Randy, you have to visualize the the relations between your Objects. Rectangle and Circle inhert from Shape. So both, Circle and Rect, can be a Shape. Because of that it is possible to change your drawObject from Rectangle to a Circle but the declaration of multiple variables with the same name is wrong. Your Object drawObject in general is just a pointer on thr Stack pointing to an Object ( Rectangle or Circle) on the Heap. This variable always have to be unique so you can change the Object they are referencing but never have the same Variable twice
Upvotes: 1
Reputation:
Its not about eclipse, java does not allows duplicate names in the same scope. Since you have both lines in same block {}
here its main()
, hence you have error pointed out by your IDE.
How to resolve?
Option 1: Use different blocks
{
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
}
{
Shape drawObject = new Circle(40);
drawObject.draw();
}
Option 2: Use same variable.
Shape drawObject;
drawObject = new Rectangle(40,60);
drawObject.draw();
drawObject = new Circle(40);
drawObject.draw();
Option 3: Use different variable names
Upvotes: 0
Reputation: 196
You recalled Shape again, here would be the appropriate fix
Edited to give him a better solution
public class TestPolymorph {
public static void main(String[] args) {
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
drawObject = new Circle(40);
drawObject.draw();
}
}
Upvotes: 0
Reputation: 9819
You are declaring the variable twice. Instead, override it by removing the type declaration at the second instantiation:
public class TestPolymorph {
public static void main(String[] args) {
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
drawObject = new Circle(40);
drawObject.draw();
}
}
I do encourage you to change the name to a value that makes more sense though:
public class TestPolymorph {
public static void main(String[] args) {
Shape rectangleShape = new Rectangle(40,60);
rectangleShape.draw();
Shape circleShape = new Circle(40);
circleShape.draw();
}
}
Upvotes: 12
Reputation: 2092
public class TestPolymorph {
public static void main(String[] args) {
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
drawObject = new Circle(40);
drawObject.draw();
}
}
Upvotes: 1
Reputation: 776
You can define it without creating new object:
Shape drawObject = new Rectangle(40,60);
drawObject.draw();
drawObject = new Circle(40);
drawObject.draw();
After this code, drawObject references a Circle type object.
Upvotes: 2
Reputation: 2972
Once you define the datatype for an Object if you want to re use the variable you should simply assign the new value to the variable (without using the data type). In your case:
int i = 0;
int i = 1; \\it won't work. because you already define the datatype
Instead:
int i = 0;
i = 1; \\This will work. you don't have to define the datatype again for the same variable
Use drawObject = new Circle(40);
for second time
Upvotes: 1