Assume that Shape is an interface. Variable shape is defined to be of type Shape. An assignment statement has variable shape on the left hand side. Assume that classes Circle and Rectangle both implement interface Shape. Describe what kind of objects can be assigned to variable shape on the right-hand side of the assignment statement?
Shape shape;
shape = _____???______
Any concrete classes that implement an interface I, exhibit an is-a relationship with the interface (similar to the is-a relationship with parent classes).
Thus, here each of class Circle and Rectangle is-a Shape.
Thus,
On the right side, any/all instance(s) of Circle/Rectangle can be written.
Example :
Circle c = new Circle();
Rectangle r = new Rectangle();
Shape s = null;
s = c; // valid
s = r; // valid
s = new Rectangle(); // valid
s = new Circle(); // valid
Get Answers For Free
Most questions answered within 1 hours.