0

I trying to create a 2d shapes for my assignment. For that I will first creating an abstract Shape2d class. My assignment clearly indicates that I need to override the equals(Object o) method in the object class which will compare 2d shapes. I made the comparisons with comparing the shapes area and the perimeter disrespect to position in the 2d plane. But the problem here is, I cannot use the abstract methods of the Abstract class Shape2d. Since I need to override the method equals(Objcet o) I cannot convert into equals(Shape2d o). What I can I do with that ? There is my code in the below.

/** This class is an abstract class and it is created for to be inherited specialized 2d shapes
     *  @version 02.26.2021
     */
    public abstract class Shape2d 
    {
        private int xLocation;
        private int yLocation;
    
        public Shape2d(int xLocation, int yLocation)
        {
            this.xLocation = xLocation;
            this.yLocation = yLocation;
        }
    
        /** Getting the yLocation
    
         *  @version 02.26.2021
         *  @return
         */
        public int getYLocation()
        {
            return yLocation;
        }
    
        /** Getting the xLocation
    
         *  @version 02.26.2021
         *  @return
         */
        public int getXLocation()
        {
            return xLocation;
        }
    
        /** Abstact method which will be used for other 2d objects. Calculating the area of the 2d shape
    
         *  @version 02.26.2021
         *  @return
         */
        public abstract double calculateArea();
    
        /** Abstact method which will be used for other 2d objects. Calculating the perimeter of the 2d shape 
    
         *  @version 02.26.2021
         *  @return
         */
        public abstract double calculatePerimeter();
    
        /** This method will calculate the distance of the two objects in absolute values.
    
         *  @param anyShape
         *  @return
         */
        public double calculatingDistance(Shape2d anyShape)
        {
            if(anyShape instanceof Shape2d)
            {
                return Math.sqrt(Math.abs(Math.pow(yLocation - anyShape.yLocation,2) + Math.pow(xLocation - anyShape.xLocation, 2)));
            }
    
            return -1;    
        }
    
        @Override
        /** This method is Overrided. It will return the Cordinates of the center (x,y)
    
         *  @version 02.26.2021
         */
        public String toString()
        {
            return "Center Cordinates of the Shape is: (" + xLocation +"," + yLocation + ")\n";
        }
    
    
        /** This method is overriding the equals method for finding out whether or not this shapes are the same
         *  disrespect to its position in the 
    
         *  @version 02.26.2021
         *  @param anyShape
         *  @return
         */
        public boolean equals(Shape2d anyShape)
        {
            return calculateArea() == anyShape.calculateArea() && calculatePerimeter() == anyShape.calculatePerimeter();
        }
        
    
    
    }
9214
  • 2,105
  • 11
  • 15
asimtot
  • 63
  • 1
  • 6
  • 1
    You override `equals (Object)`, check if the given argument value is a shape, and if so, cast. – daniu Feb 26 '21 at 13:24
  • 1
    If you want to *override* a method, it has to match the return type and the formal parameters list of the method you are overriding. You have to modify your `equals()` to accept an object of type `Object`, not `Shape2d`. – Janez Kuhar Feb 26 '21 at 13:28
  • Related: https://stackoverflow.com/questions/2698419/java-abstract-class-equals-and-two-subclasses – M A Feb 26 '21 at 13:29

1 Answers1

1

equals accepts an Object. Hence you must use public boolean equals(Object o). The parameter types are part of the method signature. If you use other parameter types (Shape2d instead of Object), then it is a method overload instead of an override.

However, within the method's body, you can typecast to an instance of a Shape2d. Of course, we first have to check whether o is actually an instance of Shape2d, using instanceof.

if (o instanceof Shape2d) {
    Shape2d s = (Shape2d) o;
    // Do something with s
}

Note that many equals implementations use this.getClass() == o.getClass().

As already mentioned in the comments, the compiler only allows to cast to classes which are compatible to the type subjected to the casting.

  • An upcast is always valid, but (almost) never necessary. For example, Object o = (Object) someStringInstance.
  • A cast to itself is always valid, but never necessary. For example, String s = (String) someStringInstance.
  • A downcast, that is a cast to a more specific type, is always valid, provided that the target type is actually a subtype of the subject type.
  • If the source type and target type are unrelated, then the compiler will disallow this, and emits a compile error. For instance, LocalTime time = (LocalTime) someStringInstance will emit a compile error.
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • In java, can I typecast any objects into any other objects and make them behave with other methods in the **typecasted** class? – asimtot Feb 26 '21 at 13:30
  • 1
    @Asimtot there are some restrictions where the compiler won't let you cast at all, and even if it does, if you cast to an invalid class, you will get an exception at runtime. – daniu Feb 26 '21 at 13:33
  • 1
    @Asimtot, no, you can successfully cast references only to types compatible with the class of the object. That's the reason for the `instanceof` test in the example. In case it's not obvious, when that `instanceof` evaluates to `false`, you would want `equals()` to return `false`, all other considerations aside. – John Bollinger Feb 26 '21 at 13:35