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

  • Home
  • SEARCH
  • 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 6620253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:04:28+00:00 2026-05-25T21:04:28+00:00

abstract class Type<K extends Number> { abstract <K> void use1(Type<K> k); // Compiler error

  • 0
abstract class Type<K extends Number> {
    abstract <K> void use1(Type<K> k);           // Compiler error (Type parameter K is not within its bounds)
    abstract <K> void use2(Type<? extends K> k); // fine
    abstract <K> void use3(Type<? super K> k);   // fine
}

The method generic type K shadows the class generic type K, so <K> doesn’t match <K extends Number> in use1().The compiler doesn’t know anything usefull about new generic type <K> in use2() and use3() but it is still legal to compile . Why <? extends K> (or <? super K>) match <K extends Number>?

  • 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-25T21:04:29+00:00Added an answer on May 25, 2026 at 9:04 pm

    First of all, let’s rewrite it to avoid shadowing:

    abstract class Type<N extends Number> {
        abstract <K> void use1(Type<K> k); 
        abstract <K> void use2(Type<? extends K> k); 
        abstract <K> void use3(Type<? super K> k);   
    }
    

    In the first method K acts as a type parameter of Type<N extends Number>, thus its value sould comply to the bound of Type‘s N. However, method declaration doesn’t have any restrictions on value of K, therefore it’s not legal. It would be legal if you add a necessary restriction on K:

    abstract <K extends Number> void use1(Type<K> k);
    

    In the following methods, the actual type parameter of Type is unknown (?), and K imposes additional bound on it, so that there is nothing illegal in these declarations.

    Here is a more practical example with the similar declarations:

    class MyList<N extends Number> extends ArrayList<N> {}
    
    <K> void add1(MyList<K> a, K b) {
         a.add(b); // Given the method declaration, this line is legal, but it 
              // violates type safety, since object of an arbitrary type K can be
              // added to a list that expects Numbers
              // Thus, declaration of this method is illegal
    }
    
    <K> void add2(MyList<? extends K> a, K b) {
         // a.add(b) would be illegal inside this method, so that there is no way
         // to violate type safety here, therefore declaration of this method is legal
    }
    
    <K> void add3(MyLisy<? super K> a, K b) {
         a.add(b); // This line is legal, but it cannot violate type safey, since
              // you cannot pass a list that doesn't expect K into this method
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Within my code a have the following abstract superclass public abstract class AbstractClass<Type extends
I have an abstract class: type TInterfaceMethod = class abstract public destructor Destroy; virtual;
I have an abstract base class and derived class: type TInterfaceMethod = class public
public abstract class Master { public void printForAllMethodsInSubClass() { System.out.println (Printing before subclass method
Given this: abstract class ViewPresenterPair { type V <: View type P <: Presenter
So, I have an abstract class like: public abstract class AbstractParent <E extends Enum<E>>
public abstract class ASTNode3 extends ASTNode { ASTNode child1; ASTNode child2; ASTNode child3; public
I have an abstract generic class. public abstract class FieldHandlerWithData<DataType extends Parcelable> extends FieldHandler
I have an abstract class. I'm extending that class. I'm getting this error: Fatal
I have created an abstract class FbActivity, which extends Activity to connect to facebook

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.