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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:05:40+00:00 2026-06-01T12:05:40+00:00

First of all, my ubiquitous language is PHP, and I’m thinking about learning Java.

  • 0

First of all, my ubiquitous language is PHP, and I’m thinking about learning Java.

So let me split my question on two closely related parts.

Here goes the first part.

Say I have a domain-model class. It has some getters, setters, some query methods etc. And one day I want to have a possibility to compare them. So it looks like:

class MyEntity extends AbstractEntity
{
    public function getId()
    {
        // get id property
    }

    public function setId($id)
    {
        // set id property
    }

    // plenty of other methods that set or retrieve data

    public function compareTo(MyEntity $anotherEntity)
    {
        // some compare logic
    }
}

If it would have been Java, I should have implemented a Comparable interface. But why? Polymorphism? Readbility? Or something else? And if it was PHP — should I create Comparable interface for myself?

So here goes the second part.

My colleague told me that it is a rule of thumb in Java to create an interface for every behavioral aspect of the class. For example, if I wanted to present this object as a string, I should state this behaviour by something like implements Stringable, where in case of PHP Stringable would look like:

interface Stringable
{
    public function __toString();
}

Is that really a rule of thumb? What benefits are gained with this approach? And does it worth it in PHP? And in Java?

  • 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-01T12:05:42+00:00Added an answer on June 1, 2026 at 12:05 pm

    If it would have been Java, I should have implemented a Comparable interface. But why?

    Well, you’re already providing a way of comparing “this” with another instance… if you implement Comparable, you’re telling the rest of the system that you support that ability, so that the comparison can be using for sorting, finding the “maximum” or “minimum” value etc… all in routines which know nothing about your type.

    My colleague told me that it is a rule of thumb in Java to create an interface for every behavioral aspect of the class.

    That sounds somewhat overboard to me. (Your example isn’t a good one as there’s already toString() on Object.) Don’t randomly create interfaces for no purpose – but create them if you want that behaviour to be used by code which doesn’t need know about your concrete type, only about what it supports. A good example of this is testability – if you write an “authenticator” class, then you can imagine either swapping different authenticators in your production code or mocking/faking the authenticator when you’re testing other components which use authentication.

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

Sidebar

Related Questions

first of all, sorry if that question is dumb but I´m a total newbie
First of all, for the sake of clarity: the question is addressed to those
First of all, let me clarify that I know you cannot have actual abstract
First of all, I am sorry if this question doesn't belong to SO since
First all, I am still learning CakePHP and I am close to ZERO using
First of all, I know how to build a Java application. But I have
First of all I have seen that there are many questions about unrecognized selector
I dig a lot of things about the DDD approach (Ubiquitous language, Aggregates, Repositories,
First of all there is a partial question regarding this, but it is not
First of all, I know I asked a similar question before but this one

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.