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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:28:50+00:00 2026-06-02T20:28:50+00:00

I have an question about the following code (Is this call dynamic binding?). I

  • 0

I have an question about the following code (Is this call dynamic binding?). I feel confused about 3 point.

First, what is the mean of the variable pq? Does pd still be the data type of P or be the Q?

Second, when I invoke the pq.m(pp) method, why the result become Q::P but not P::Q?

Finally, what is this mean ((P) qq).m(qq);? I hope somebody could solve my problem.

The result of the following code will be
P::Q, Q::P, Q::Q, R::P, Q::P, Q::Q, Q::Q

    class Test {
    public static void main(String[] args) {
        P pp  = new P();     
        Q qq  = new Q();
        R rr = new R();
        P pq  = qq;
        pp.m(qq);    
        pq.m(pp);  
        pq.m(qq);         
        rr.m(pp); 
        qq.m(pq);   
        qq.m(qq); 
        ((P) qq).m(qq);      
    }
}
class P {
    public void m(P p){System.out.println("P::P"); } 
    public void m(Q p){System.out.println("P::Q"); }
    public void m(R c){System.out.println("P::R"); }
}
class Q extends P {
    public void m(P p){System.out.println("Q::P"); } 
    public void m(Q p){System.out.println("Q::Q"); }
    public void m(R c){System.out.println("Q::R"); }
}
class R extends Q {
      public void m(P p){System.out.println("R::P"); } 
      public void m(Q p){System.out.println("R::Q"); }
    public void m(R c){System.out.println("R::R"); }
}
  • 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-02T20:28:52+00:00Added an answer on June 2, 2026 at 8:28 pm

    P pq = qq; means that pq is known to the rest of the program as a type P. But as the creator, you know it’s really of type Q. So this means that when you call pq.m(), it’s really calling the implementation from class Q.

    It’s called overriding a method. So when you call pq.m(pp), you are really calling:
    public void m(P p){System.out.println("Q::P"); because that is the method from class Q.

    If Q did not have a m(P) method, then it would automatically call the superclass method, the one from P.

    ((P) qq).m(qq); is the same as doing:

    P pqq = (P)qq;   // pqq is known as P type, but it's instance is still the original Q type
    pqq.m(qq); // Again, since pqq is truly an instance of Q, it calls Q.m(Q)
    

    You should really read about inheritance. This is a bigger subject than can be explained here.

    All this being said, your example doesn’t illustrate its power well. But for example, if class Q had an extra method, public void sayHello();, then

    Q q = new Q();
    P p = new Q();
    q.sayHello(); // This would be legal
    p.sayHello(); // This would be illegal because the compiler knows p as a declared instance of P, even though you know it's truly a Q.
    ((Q)p).sayHello(); // This would be legal because you told the compiler to look at p as an instance of Q. It's called a cast.
    

    I hope this all helps. Be sure to go read up on object orientation.

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

Sidebar

Related Questions

I have a question about the following code : #include <iostream> #include <ctime> int
I have a question about the following C code: void my_function() { int i1;
I have a question about the following code: #include <iostream> #include <boost/scoped_ptr.hpp> class Interface
I have a question about the following code which I'm using in a prepare-execute
I have the following question about JPA: Can I save the order of the
I have a question about jQuery load best practice. Imagine the following situation: $(#div).on('click',
I might have pretty basic question about regex. I have the following regex, which
Following on from a question I posted yesterday about GUIs, I have another problem
I'm sorry about the naive question. I have the following: public Array Values {
I have question about normalization. Suppose I have an applications dealing with songs. First

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.