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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:58:55+00:00 2026-05-25T12:58:55+00:00

Here are two examples: public class A { public void foo(A a) { System.out.println(in

  • 0

Here are two examples:

public class A {
    public void foo(A a) {
        System.out.println("in A");
    }
}


public class B extends A {      
    public void foo(B b) { // (1)
        System.out.println("in B");
    }
    public void f(Object o) { // (2)
        System.out.println("in B");
    }
}

I don’t understand how come (1) and (2) are considered to be an overrided method for A’s foo(). method number 1 accepts a lower class than the original foo(), I mean that I can’t send her any class of A. as I see it, (1) does not extend foo(), but it is still counts as an override- why?( Similar argument for (2), but opposite).

The reason that I made me think that it is overrided is this:

when I’m trying to run

  B b = new B();

  b.foo(b);

It checks if A has a foo that accepts B. since every B is an A, it does have one so it ought to print “in A” and not “in B” because B does not overrides it. but it prints “in B”..

  • 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-25T12:58:56+00:00Added an answer on May 25, 2026 at 12:58 pm

    Neither of them override the super class A’a method.

    class A {
      public void foo(A a) {
        System.out.println("in A");
      }
    }
    
    
    class B extends A {
    
      @Override
      public void foo(B b) {
        System.out.println("in B");
      }
      @Override
      public void foo(Object o) {
        System.out.println("in B");
      }
    }
    

    When I compile the above I get errors:

    $ javac -g O.java 
    O.java:10: method does not override or implement a method from a supertype
      @Override
      ^
    O.java:14: method does not override or implement a method from a supertype
      @Override
      ^
    2 errors
    

    But note that it is ok to return a subclass from an overriding method. The following compiles without error.

    class A {
      public A foo() {
        System.out.println("in A");
        return this;
      }
    }
    
    class B extends A {
      @Override
      public B foo() {
        System.out.println("in B");
        return this;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this example: class One { public void testOne(){System.out.println(One!!!);} public void testTwo(){System.out.println(One!!!);} }
If you want to associate some constant value with a class, here are two
For Example I have two intefaces and class type: class IPlugin { public: virtual
Here are two different questions but I think they are related. When using Git,
Here are two chunks of code that accomplish (what I think is) the same
Here are two questions related to modifying the data source for strongly typed dataset
I have a very strange behavior with Request.Form . Here are two IIS 7
First.. here are the two tables I've created (sans irrelevant columns).. CREATE TABLE users_history1
Here's the thing: I have two applications, written in C++ and running on two
Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx

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.