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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:24:02+00:00 2026-06-11T00:24:02+00:00

I want to make an interface that forces each class that implements it to

  • 0

I want to make an interface that forces each class that implements it to have a certain functionality, for the implemented class’ type.

So say I have classes MyClassA, MyClassB, MyClassC, etc. that all need a function on their own type:

in MyClassA:

public class MyClassA implements MyClass {
    MyClassA function(MyClassA x) {
        doSomethingImplementedInMyClassA(x);
    }
}

in MyClassB:

public class MyClassB implements MyClass {
    MyClassB function(MyClassB x) {
        doSomethingImplementedInMyClassB(x);
    }
}

The question is, how to write the interface MyClass to require such function?

public interface MyClass {
    MyClass function(MyClass x);
}

obviously doesn’t work, since the returning type is MyClass and not its implementation. How to do this properly 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-11T00:24:04+00:00Added an answer on June 11, 2026 at 12:24 am

    You can use generics:

    public interface MyClass<V extends MyClass<V>> {
        V function(V x);
    }
    
    public class MyClassA implements MyClass<MyClassA> 
    

    This is called the CRTP.


    This is not perfect; it would still allow things like

    public class MyClassB implements MyClass<MyClassA> 
    

    To do this correctly, you need higher-kinded types [citation needed], which Java does not support.

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

Sidebar

Related Questions

I want to make an interface in C# that defines a method that always
I have style sheet with a class name changebackgroundcolor i want make change in
I have an interface so class writers are forced to implement certain methods. I
So, say I have a simple enum and a class that uses it: enum
I want to make an interface that looks like this So here is what
I want to make a web interface that prints out a form that needs
as the title says, I want to make a tab interface where I have
I want to make an interface that looks like Outlook. In the center pane,
While certain guidelines state that you should use an interface when you want to
I'm messing around with Xcode and Interface Builder. I want to make a timer

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.