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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:22:59+00:00 2026-06-04T01:22:59+00:00

I have a Student class and a StudentPool class defined as follows: public class

  • 0

I have a Student class and a StudentPool class defined as follows:

public class Student {
    public Student copy () {
        return new Student();
    } 
}

and

public class StudentPool<T extends Student> {
    public T copyStudent(T t) {
        return t.copy();
    }
}

thus, the copyStudent method cannot be compiled and I have to use un-safe type casting. I cannot understand why Java does regard this as illegal?

Edit:
user845279, lcfseth, and Bohemian: I think the following revision to Student may cause the similar scenario, cast a parent class into its child class, but this version can pass compiling:

public class Student {
    public <T extends Student> T copy() {
       T rv = null; 
       return rv;
    } 
}

Edit:
forget the above code: rv can be either null or un-safely casted.

  • 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-04T01:23:00+00:00Added an answer on June 4, 2026 at 1:23 am

    The problem is that although Student returns Student from copy(), subclasses would also return Student… not their own type.

    Here’s an approach to fix your problem:

    public static interface Copyable<T> {
        public T copy();
    }
    
    public static class Student implements Copyable<Student> {
        @Override
        public Student copy() {
            return new Student();
        }
    }
    
    public static class StudentPool<T extends Student> {
        public T copyStudent(Copyable<T> t) {
            return t.copy();
        }
    }
    

    As a side note, it seems you don’t need generics here.

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

Sidebar

Related Questions

I have a Student class in my application as below: public class Student {
I have class Student { public List<Degree> Degrees {get;set;} } class Degree { public
Suppose I have two classes: public class Student { public int Id {get; set;}
I have the following piece of code: class Student { public: Student(){} void display()
Say i have a object like this public class Student{ public IList<Coursework> Courseworks{get;set;} public
I have created a class student with three properties like this public class Student
Let's say you have a class object: public class Class { IList<Student> students; }
I have a class that looks like this class Student { public String str;
I have this schema : public class Student { public String name; public School
I have the following three classes: public class Student { private Integer studentId; private

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.