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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:59:41+00:00 2026-05-30T16:59:41+00:00

I was trying following example: class BaseClass { public void methodA(Class<?> cl) { System.out.println(Base.methodA());

  • 0

I was trying following example:

class BaseClass {
    public void methodA(Class<?> cl) {
        System.out.println("Base.methodA()");
    }
}

class SubClass extends BaseClass {
    public void methodA(Class cl) {
        System.out.println("Sub.methodA()");
    }
}

public class OverrideEx {

    public static void main(String[] args) {
        BaseClass b = new BaseClass();
        BaseClass s = new SubClass();

        b.methodA(Class.class);
        s.methodA(Class.class);
    }
}

Output:
Base.methodA()
Sub.methodA()

But if I change over riding method argument other way around as follows:

class BaseClass {
    public void methodA(Class cl) {
        System.out.println("Base.methodA()");
    }
}

class SubClass extends BaseClass {
    public void methodA(Class<?> cl) {
        System.out.println("Sub.methodA()");
    }
}

public class OverrideEx {

    public static void main(String[] args) {
        BaseClass b = new BaseClass();
        BaseClass s = new SubClass();

        b.methodA(Class.class);
        s.methodA(Class.class);
    }
}

I get compilation error. It says “Name clash: The method methodA(Class) of type SubClass has the same erasure as methodA(Class) of type BaseClass but does not override it”.

Why is that?

  • 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-30T16:59:42+00:00Added an answer on May 30, 2026 at 4:59 pm

    The parameterized type Class<?> is a subtype of the raw type Class (§4.10.2).

    Thus for any x the call s.methodA(x) in the first example would also be valid if rewritten:

    ((BaseClass)s).methodA(x)
    

    This is not true for the second example.

    Imagine x being of type Class which is not a subtype of Class<?>. That call
    would be illegal because the argument’s type is not a subtype of the formal parameter’s type.

    This means that methodA of SubClass does not override methodA of BaseClass.

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

Sidebar

Related Questions

I'm trying to implement a delegate class following Herb Sutter's Example . There is
I'm trying to figure out how XSLT process namespace prefixes and have following example:
Take the following code as an example: class xpto { public function __get($key) {
I have the following example relationship: namespace Yesod { public class Program { //
In the following example, I'm trying to use sun.tools.javac.Main to dynamically compile a class
I was trying the following example, but with external URLs: Using WebViews The example
I was trying the following example of I/O ports from here . #include<stdio.h> #include<unistd.h>
I'm trying to access $a using the following example: df<-data.frame(a=c(x,x,y,y),b=c(1,2,3,4)) > df a b
I am trying to create dynamic menus from the database using the following example
Trying to use the following demo example, using the 80px x 80px snap to

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.