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 8690715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:57:41+00:00 2026-06-12T23:57:41+00:00

Return an immutable interface to the original data. You can then change fields in

  • 0

“Return an immutable interface to the original data. You can then change fields in the object, but the caller cannot unless he cheats by casting. You expose only the methods you want the user to have. Doing the same with classes is trickier since a subclass must expose everything its superclass does”

What does he mean you can cheat, and why is it tricky with subclasses ?

source: http://mindprod.com/jgloss/immutable.html

  • 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-12T23:57:42+00:00Added an answer on June 12, 2026 at 11:57 pm

    You provide an interface that has no mutation methods. Then, you provide mutable implementations that are only known to the creator.

    public interface Person
    {
        String getName();
    }
    
    public class MutablePerson implements Person
    {
        private String name;
    
        public MutablePerson(String name)
        {
            this.name = name;
        }
    
        @Override
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    }
    

    At this point, if you return Person objects everywhere, the only way for someone to modify the returned object is to cheat and cast it back to a MutablePerson. In effect, the mutable objects become immutable unless the code is a complete hack.

    Person person = new MutablePerson("picky");
    // someone is cheating:
    MutablePerson mutableAgain = (MutablePerson)person;
    mutableAgain.setName("Phoenix");
    
    // person.getName().equals("Phoenix") == true
    

    When not dealing with a bunch of younger programmers that will notice the true implementation is mutable, and thus they can cast it to change it, then you provide the safety of immutability with the benefit of being able to put it together without an endless constructor, or using a Builder (in effect, the mutable version is the Builder). A good way to avoid developers abusing the mutable version is to leave the mutable version as package private so that only the package knows about it. The negative of that idea is that this only works if it will be instantiated in the same package, which may be the case, but it obviously may not be the case in situations such as where DAO’s are used with multiple package-defined implementations (e.g., MySQL, Oracle, Hibernate, Cassandra, etc., all returning the same stuff, and hopefully separated from each other to avoid cluttering their packages).

    The real key here is that people should never build up from the Mutable objects except to implement further-down interfaces. If you’re extending, and then returning an immutable subclass, then it’s not immutable if it exposes a mutable object, by definition. For example:

    public interface MyType<T>
    {
        T getSomething();
    }
    
    public class MyTypeImpl<T> implements MyType<T>
    {
        private T something;
    
        public MyTypeImpl(T something)
        {
            this.something = something;
        }
    
        @Override
        public T getSomething()
        {
            return something;
        }
    
        public void setSomething(T something)
        {
            this.something = something;
        }
    }
    
    public interface MyExtendedType<T> extends MyType<T>
    {
        T getMore();
    }
    
    public class MyExtendedTypeImpl<T>
            extends MyTypeImpl<T>
            implements MyExtendedType<T>
    {
        private T more;
    
        public MyExtendedTypeImpl(T something, T more)
        {
            super(something);
    
            this.more = more;
        }
    
        @Override
        public T getMore()
        {
            return more;
        }
    
        public void setMore(T more)
        {
            this.more = more;
        }
    }
    

    This is honestly the way that Collections in Java should have been implemented. A readonly interface could have taken the place of the Collections.unmodifiable implementations, thus not having people unexpectedly using immutable versions of mutable objects. In other words, you should never hide immutability, but you can hide mutability.

    Then, they could sprinkle immutable instances that truly can’t be modified, and that would keep developers honest. Similarly, I would likely expect to see an immutable version of the above interface somewhere (with better names):

    public class MyTypeImmutable<T> implements MyType<T>
    {
        private final T something;
    
        public MyTypeImmutable(T something)
        {
            this.something = something;
        }
    
        @Override
        public T getSomething()
        {
            return something;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like to make data classes immutable to make concurrent programming easier. But making
If I want to return an immutable array like this + (NSArray *)ids but
I have class, that contains different types and implements Iterable<Object> interface. But it is
return false seems to work in the submitHandler but I'm not sure why. function
Why return type of a method is not considered in method overloading ? Can
i return such type IQueryable< IGrouping<int, Invoice>> List() how can i work with it?
...and return the number as string. How can I do that? Is there any
You often read about immutable objects requiring final fields to be immutable in Java.
Strange things happens to me, I try to create mutable dictionary, but it return
I'm interested in making an immutable class that has properties that cannot be modified,

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.