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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:21:03+00:00 2026-06-08T02:21:03+00:00

Requirement: I’d like all implementations of an interface to have a well-defined name. Initially,

  • 0

Requirement: I’d like all implementations of an interface to have a well-defined name.

Initially, I thought:

interface Fruit {
    public String getName();
}

But this allows the user to have a field that is modified at run-time. I want to have an immutable name that is defined before compile/build time.

I’ve been toying with a couple of other ways to do it, but each has a limitation.

1) Give the name a type, which has slightly more control than free-form strings:

interface Fruit {
    public FruitName getName();
}

abstract class FruitName  {
    public final String NAME;
    public FruitName(name) {
        this.NAME = name;
    }
}

A user of this class will look like this:

class AppleFruitName extends FruitName {
    public AppleFruitName() {
        super("apple");
    }
}

class Apple implements Fruit {
    public FruitName getName() {
        return new AppleFruitName();
    }
}

2) Force an implementor of Fruit to annotate the name with something:

class Apple implements Fruit {
    @FruitName
    public static final NAME = "apple";
    ...    
}

Clearly this implementation is far cleaner than (1), but I’m not sure if this is possible in Java? How do you get compile/build to fail if @FruitName is not present?

  • 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-08T02:21:04+00:00Added an answer on June 8, 2026 at 2:21 am

    An easy way to do this – without aop, compile time weaving, runtime annotations, scanning at runtime.. etc is to encapsulate this behaviour in an abstract class:

    interface Fruit {
      public String getName();
    }
    
    abstract class FruitImpl  {
      private final String name;
      public FruitImpl(name) {
        this.name = name;
      }
    
      public final String getFruitName(){
        return name;
      }
    

    }

    So at construction time each implementation will be forced to pass in its name and it will not be able to alter it (unless the user is being intentionally malicious). This meets the what the wording of the question suggests.

    There is a difference though because some the suggestions seem to assume that all implementations of the interface will have the same name – though the question doesn’t state that. Is the idea that these implementations will be singletons?

    Alternatively, you could use the decorator pattern to wrap the implementation and retrieve the field value once and then always return that value later, like this:

    class FruitWrapper implements Fruit{
      private final String name;
      public FruitWrapper(Fruit fruit) {
        this.name = fruit.getFruitName();
      }
    
      public final String getFruitName(){
        return name;
      }
    

    }

    So you can use it everywhere you would use fruit and it will guarantee to always get the same value.

    This way you move the immutability into a class you control.

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

Sidebar

Related Questions

Requirement: I would like to interface my .NET application to a virtual webcam driver
Requirement : Get like count of all pages under my FB account. FQL https://api.facebook.com/method/fql.query?query=SELECT+user_id,object_id,post_id+FROM+like+WHERE+user_id=me()
My requirement is that user performing alter CANNOT be sysadmin (it can have all
We have a requirement in project to store all the revisions(Change History) for the
Requirement I have an application with 2 activities, say A and B , with
Requirement is this ... We have our 3 web-applications deployed in RHEL-5 server, we
Requirement is like: I want to take input values from user in frame but
Requirement of my app is to have UINavigationController at the bottom with play, rewind
Requirement: select by date as After, Before, Between or all if null I'm using
Requirement:- File1 has contents like - ABCD00000001,\some\some1\ABCD00000001,Y,,5 (this indicates there are 5 file in

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.