Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

19/20 Sem 2 Q1 - Polymorphism #550

Open
ZhangAnli opened this issue Nov 29, 2020 · 6 comments
Open

19/20 Sem 2 Q1 - Polymorphism #550

ZhangAnli opened this issue Nov 29, 2020 · 6 comments
Labels
🖱️ Java General Questions with regards to Java or its API

Comments

@ZhangAnli
Copy link

Description

Hi can anyone explain why the question below has an answer of "1534"? I thought type checking happens during runtime and therefore had an answer "1555". :(

Topic:

Finals

Screenshots (if any):

Screenshot 2020-11-29 at 3 52 36 PM

@ZhangAnli ZhangAnli added the 🖱️ Java General Questions with regards to Java or its API label Nov 29, 2020
@kohkenneth
Copy link

Hi Anli, I think you can refer to this post for some inspiration! #544

@remuskwan
Copy link

Hi, static binding occurs during method overloading. So the method to be called is decided during compile time. In the case of line 3, ab is of type A during compile time as it is declared to be so, thus the method foo(A a) in class B is called. In line 4, i is of type I during compile time as it is declared to be so, thus the method foo(I i) is called. Hope this helps!
Screenshot 2020-11-29 at 5 07 55 PM

@KongXinyue
Copy link

b.foo(ab) refers to the method in class B, which takes in a A object
b.foo(i)refers to the method in class B, which takes in I

@wongzw
Copy link

wongzw commented Nov 29, 2020

Hi @ZhangAnli,

b.foo(ab) means that you are calling the method with the input of Type A in Class B -> Printing of 3
b.foo(i) means that you are calling the method with the input of Type I in Class B -> Printing of 4

Below is my code for reference :)

 interface I {
      void foo(A a);
      void foo(I i);
 }

class A implements I{
    public A(){};

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

    public void foo(I i) {
        System.out.println("2");
    }
}


class B extends A implements I{
    public B(){};

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

    public void foo(I i) {
        System.out.println("4");
    }

    public void foo(B b) {
        System.out.println("5");
    }
}

@nowknowing
Copy link

Look recitation 02 question1. There's also supplementary materials from that recitations for more explanations.

@pikasean
Copy link

I think this is a general framework for these type of questions, e.g. x.foo(y)

  1. Determine compile time type of x and y.
  2. Check overloaded methods.
  3. Check overridden methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖱️ Java General Questions with regards to Java or its API
Projects
None yet
Development

No branches or pull requests

7 participants