Inheritance and the "is-A" connection

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.
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;
   }
}
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top