- Joined
- Jun 29, 2022
- Messages
- 28
- Reaction score
- 0
I have a question about the Java "is-A" connection and how it relates to interfaces. To help clarify my query, I've defined an interface and a class that implements it below.
So, because Circle implements Shape, we may state that Circle is-A Shape and so utilize a Circle object wherever a Shape is expected, exactly as a line1 does.
When I write public int methods (Circle other) for line 2, however, I receive a build time error stating that it cannot override the method in Shape.
I read various articles online but this (link) is the only one that has some info I need to ask again just to be sure. Could someone perhaps explain why the same rationale does not apply to overriding interface methods? Because a circle is a Shape, why can't I just say public int methods? (Circle other).
Thank You!
Code:
public interface Shape
{
int method(Shape other);
}
public class Circle implements Shape
{
private Shape s = new Circle(); //line 1
public int method(Shape other) //line 2
{
return 3;
}
}
When I write public int methods (Circle other) for line 2, however, I receive a build time error stating that it cannot override the method in Shape.
I read various articles online but this (link) is the only one that has some info I need to ask again just to be sure. Could someone perhaps explain why the same rationale does not apply to overriding interface methods? Because a circle is a Shape, why can't I just say public int methods? (Circle other).
Thank You!