Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:58:45+00:00 2026-06-06T06:58:45+00:00

Say that I have an immutable Point class with x and y parameters, and

  • 0

Say that I have an immutable Point class with x and y parameters, and an add method defined like this:

class Point:
  Point add(int x, int y):
    return new Point(this.x + x, this.y + y);

Since it’s immutable, it returns a new Point. This is all well and good until we have a class that extends Point and redefines add.

class ColoredPoint extends Point:
  ColoredPoint add(int x, int y):
    return new ColoredPoint(this.x + x, this.y + y, this.width, this.height)

We have to write the new definition because otherwise the add method on ColoredPoint would return Point which is bad. But now this won’t work in most languages because we’re overriding add but we’re only disambiguating on return type. Languages don’t allow you to have methods that are only disambiguated on return type, because that can be ambiguous in most cases.

So what do we do?

The only solutions I see are:

  • Make them both implement some interface like IPositionable
  • Give up on immutability.

Anything else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:58:48+00:00Added an answer on June 6, 2026 at 6:58 am

    If you want to enforce immutability, you cannot have subclasses. See for example java.lang.String, which is a final class for this reason: To prevent people from subclassing String to make it mutable.

    Also, why would Rect extend Point in the first place? Isn’t it a completely different thing? Geometry-wise, they don’t even exist in the same dimension.

    Finally, I cannot see the connection between the problem of methods on subclasses returning a more restrained type (and some languages not supporting this well), and immutability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that i have a generic dictionary with data like this (I hope the
Let's say I have a class Foo in Java that has immutable data: class
Say I have an array of Strings declared like this: String[] strings = new
let's say I have a class that behaves as an int to the outside
Let's say I have this class: class Zoo { protected String bearName; protected Double
Let's say I have a case class that represents personas, people on different social
Say I have this Class @interface CustomClass : NSObject @property (nonatomic, strong) NSArray *
Say you have a class declaration, e.g.: class MyClass { int myInt=7; int myOtherInt;
Say that I have a data model called Widget . If I pick one
Say that you have a loop and depending on the outcome of the loop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.