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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:12:15+00:00 2026-05-11T22:12:15+00:00

This question has been asked in a C++ context but I’m curious about Java.

  • 0

This question has been asked in a C++ context but I’m curious about Java. The concerns about virtual methods don’t apply (I think), but if you have this situation:

abstract class Pet
{
    private String name;
    public Pet setName(String name) { this.name = name; return this; }        
}

class Cat extends Pet
{
    public Cat catchMice() { 
        System.out.println("I caught a mouse!"); 
        return this; 
    }
}

class Dog extends Pet
{
    public Dog catchFrisbee() { 
        System.out.println("I caught a frisbee!"); 
        return this; 
    }
}

class Bird extends Pet
{
    public Bird layEgg() {
        ...
        return this;
    }
}


{
    Cat c = new Cat();
    c.setName("Morris").catchMice(); // error! setName returns Pet, not Cat
    Dog d = new Dog();
    d.setName("Snoopy").catchFrisbee(); // error! setName returns Pet, not Dog
    Bird b = new Bird();
    b.setName("Tweety").layEgg(); // error! setName returns Pet, not Bird
}

In this sort of class hierarchy, is there any way to return this in a way that doesn’t (effectively) upcast the the object type?

  • 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-11T22:12:16+00:00Added an answer on May 11, 2026 at 10:12 pm

    If you want to avoid unchecked cast warnings from your compiler (and don’t want to @SuppressWarnings(“unchecked”)), then you need to do a little more:

    First of all, your definition of Pet must be self-referential, because Pet is always a generic type:

    abstract class Pet <T extends Pet<T>>
    

    Secondly, the (T) this cast in setName is also unchecked. To avoid this, use the “getThis” technique in the excellent Generics FAQ by Angelika Langer:

    The “getThis” trick provides a way to
    recover the exact type of the this
    reference.

    This results in the code below, which compiles and runs without warnings. If you want to extend your subclasses, then the technique still holds (though you’ll probably need to genericise your intermediate classes).

    The resulting code is:

    public class TestClass {
    
      static abstract class Pet <T extends Pet<T>> {
        private String name;
    
        protected abstract T getThis();
    
        public T setName(String name) {
          this.name = name;
          return getThis(); }  
      }
    
      static class Cat extends Pet<Cat> {
        @Override protected Cat getThis() { return this; }
    
        public Cat catchMice() {
          System.out.println("I caught a mouse!");
          return getThis();
        }
      }
    
      static class Dog extends Pet<Dog> {
        @Override protected Dog getThis() { return this; }
    
        public Dog catchFrisbee() {
          System.out.println("I caught a frisbee!");
          return getThis();
        }
      }
    
      public static void main(String[] args) {
        Cat c = new Cat();
        c.setName("Morris").catchMice();
        Dog d = new Dog();
        d.setName("Snoopy").catchFrisbee();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 153k
  • Answers 153k
  • 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 Here's a fairly basic implementation that fires PropertyChangeEvents when additional… May 12, 2026 at 10:20 am
  • Editorial Team
    Editorial Team added an answer If you're accustomed to working with a relational database, Google… May 12, 2026 at 10:20 am
  • Editorial Team
    Editorial Team added an answer I think you should separate the query from the function… May 12, 2026 at 10:20 am

Related Questions

This question has been asked in a C++ context but I'm curious about Java.
This has been asked before (question no. 308581) , but that particular question and
I feel like this one has been asked before, but I'm unable to find
Note The question below was asked in 2008 about some code from 2003. As
What logging library or approach would you recommend for this case: We want to

Trending Tags

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

Top Members

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.