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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:27:41+00:00 2026-05-13T21:27:41+00:00

Consider I have the following interface: public interface A { public void b(); }

  • 0

Consider I have the following interface:

public interface A { public void b(); }

However I want each of the classes that implement it to have a different return type for the method b().

Examples:

public class C { 
  public C b() {} 
}

public class D { 
  public D b() {} 
}

How would I define my interface so that this was possible?

  • 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-05-13T21:27:41+00:00Added an answer on May 13, 2026 at 9:27 pm

    If the return type must be the type of the class that implements the interface, then what you want is called an F-bounded type:

    public interface A<T extends A<T>>{ public T b(); }
    
    public class C implements A<C>{
      public C b() { ... }
    }
    
    public class D implements A<D>{
      public D b() { ... }
    }
    

    In words, A is declaring a type parameter T that will take on the value of each concrete type that implements A. This is typically used to declare things like clone() or copy() methods that are well-typed. As another example, it’s used by java.lang.Enum to declare that each enum’s inherited compareTo(E) method applies only to other enums of that particular type.

    If you use this pattern often enough, you’ll run into scenarios where you need this to be of type T. At first glance it might seem obvious that it is1, but you’ll actually need to declare an abstract T getThis() method which implementers will have to trivially implement as return this.

    [1] As commenters have pointed out, it is possible to do something sneaky like X implements A<Y> if X and Y cooperate properly. The presence of a T getThis() method makes it even clearer that X is circumventing the intentions of the author of the A interface.

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

Sidebar

Ask A Question

Stats

  • Questions 532k
  • Answers 531k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe you can give calendar-ui a try: http://github.com/muhku/calendar-ui May 17, 2026 at 12:03 am
  • Editorial Team
    Editorial Team added an answer public class WeirdRequirements { int m_year, m_month, m_day; public delegate… May 17, 2026 at 12:03 am
  • Editorial Team
    Editorial Team added an answer A ListBox can store more than just strings, it can… May 17, 2026 at 12:03 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider the following example. I have an interface MyInterface, and then two abstract classes
Consider the following: public interface ICleanCoder { void DoSomeCoding(object task); } public interface ICleanCoder<T>
Consider the following. You have a class that you want to serialize with XmlSerializer
I have the following API: public interface MyApi { /** * Performs some stuff.
Consider the following code. public interface IFoo { } public class Bar { public
Please consider the following three .NET types: I have an interface, an abstract class,
Consider I have the following struct : struct IDirect3D { IDirect3D() : ref_count_(0) {}
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
So I made the following test for a class Board that would to be
Please, consider the following (I'm sorry for the amount of code; but this is

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.