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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:35:18+00:00 2026-06-11T22:35:18+00:00

class A{ void m1(A a) {System.out.print(A);} } class B extends A{ void m1(B b)

  • 0
 class A{
 void m1(A a) {System.out.print("A");}
  } 
  class B extends A{
  void m1(B b) {System.out.print("B");}
  } 
  class C extends B{
  void m1(C c) {System.out.print("C");}
  }

 public class d {

 public static void main(String[] args) {

  A c1 = new C(); C c2 = new C();c1.m1(c2);

 }}

Output of this code is 'A'.

But if I modify class A as:

 class A{
 void m1(C a) {System.out.print("A");}
  }

then the output is 'C'. Could somebody please explain how is this code working?

  • 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-11T22:35:19+00:00Added an answer on June 11, 2026 at 10:35 pm

    (You’ve made this more confusing than it needs to be by using the same inheritance hierarchy for both the parameter types and the implementation. Separating those would probably help to simplify it in your mind.)

    The type of c1 is A. Therefore when working out which method signature it’s going to call, the compiler can only look at methods declared in A.

    So in the first case, the compiler is going to call m1(A); in the second case, the compiler is going to call m1(C).

    Now in the first case, the m1(A) method is never overridden, so actually the execution-time type of c1 is irrelevant.

    In the second case, m1(C) is overridden by C, so the implementation in C is called because the execution-time type of c1 is C.

    So remember:

    • Overload resolution (which method signature is called) is determined at compile-time, based on the compile-time types of both the method target and the argument expressions
    • The implementation of that method signature (overriding) is based on the actual type of the target object at execution time.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
public class Foo { public static void main(String[] args) { float f; System.out.println(f); }
class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3);
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
Java code: import javax.swing.Timer; class Main { public static void main(String args[]) { MyListener
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
Why does the below code print Main? public class Main { public static void
my code is class Alpha { public void foo() { System.out.print(Alpha ); } }
Sample 1: class Animal { public static void saySomething() { System.out.print( Gurrr!); } }
class Patmatch { static String strLine=; public static void main(String [] args) { try

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.