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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:44:36+00:00 2026-06-10T10:44:36+00:00

So, I was trying to write a method to answer one of my previous

  • 0

So, I was trying to write a method to answer one of my previous questions: How can I find out if an arbitrary java.lang.Method overrides another one? To do that, I was reading through the JLS, and there are some parts that seem to be missing in one case.

Imagine you have the following classes:

public class A<T> {
    public void foo(T param){};
}

public class B extends A<String> {
    public void foo(String param){};
}

In this case, it is quite obvious that B.foo overrides A.foo, however I don’t understand how this case fits the specification.

Regarding method overriding, the JLS §8.4.8.1 states:

An instance method m1, declared in class C, overrides another instance method m2, declared in class A iff all of the following are true:

  1. C is a subclass of A.

  2. The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

  3. Either:

    • m2 is public, protected, or declared with default access in the same package as C,or
    • m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2.

Obviously points 1 and 3 are satisfied in our case. Let’s look a bit deeper in the JLS what subsignature means. The JLS §8.4.2 says:

Two methods have the same signature if they have the same name and argument types.

Two method or constructor declarations M and N have the same argument types if all of the following conditions hold:

  1. They have the same number of formal parameters (possibly zero)

  2. They have the same number of type parameters (possibly zero)

  3. Let A1, …, An be the type parameters of M and let B1, …, Bn be the type parameters of N. After renaming each occurrence of a Bi in N’s type to Ai, the bounds of corresponding type variables are the same, and the formal parameter types of M and N are the same.

In our case, point 1 is clearly true (both have 1 argument).

Point 2 is a bit more muddy (and that’s where I’m not sure what the spec means exactly): Neither of the methods declare their own type parameters, but A.foo uses T which is a type variable that paramterizes the class.

So my first question is: in this context, do Type variables declared in the class count or not?

Ok, now let’s assume that T doesn’t count and that therefore point 2 is false (I don’t know how I could even apply point 3 in this case). Our two methods do not have the same signature, but that doesn’t prevent B.foo from being a subsignature of A.foo.

A little further in JLS §8.4.2 it says:

The signature of a method m1 is a subsignature of the signature of a method m2 if either:

  1. m2 has the same signature as m1, or

  2. the signature of m1 is the same as the erasure (§4.6) of the signature of m2.

We have already determined that point 1 is false.

The erasure signature of a method according to JLS §4.6 is a signature consisting of the same name as s and the erasures of all the formal parameter types given in s. So the erasure of A.foo is foo(Object) and the erasure of B.foo is foo(String). These two are different signatures, therefore point 2 is also false, and B.foo is not a subsignature of A.foo, and therefore B.foo does not override A.foo.

Except it does…

What am I missing? Is there some piece of the puzzle that I’m not seeing, or is the spec really not complete in this case?

  • 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-10T10:44:38+00:00Added an answer on June 10, 2026 at 10:44 am

    do Type variables declared in the class count or not?

    The elements in question are the methods, not the containing type declarations. So, no, they don’t count.

    Ok, now let’s assume that T doesn’t count and that therefore point 2 is false

    Why? They both have 0 type parameters, so it’s true.


    Step by step:

    • They have the same number of formal parameters (possibly zero)

    Both methods have 1 formal parameter. Check.

    • They have the same number of type parameters (possibly zero)

    Neither method declares a type parameter. Check.

    Let A1, …, An be the type parameters of M and let B1, …, Bn be the type parameters of N. After renaming each occurrence of a Bi in N’s type to Ai, the bounds of corresponding type variables are the same, and the formal parameter types of M and N are the same.

    Since neither method declares a type parameter there’s nothing to rename and the formal parameter types are the same — T[T=String] = String. Check.

    ⇒ B.foo(String) has the same signature as A<String>.foo(String).
    ⇒ B.foo(String) is a subsignature of A<String>.foo(String)

    Since B is a subclass of A and A<String>.foo(String) is public we can conclude B.foo(String) overrides A<String>.foo(String).

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

Sidebar

Related Questions

I am trying to write a method in Java that accepts a string as
I am trying to write a method that makes a log.txt file if one
I am trying to write a unit test for our log out method. Among
I'm trying to write a method that will compute all permutations of a power
I am trying to write a method that adapts to different types of interfaces
I'm trying to write a method that removes all non alphabetic characters from a
I'm trying to write a method like this: public static T Test<T>() { if
I'm trying to write a method that acts as a setter and takes some
I am trying to write a method, that takes a ComboBox, a DataTable and
I'm trying to write a method to reduce the size of any image 50%

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.