2

I have been doing some changes in my project where I found some weird or rather I will say unexpected behaviour of Polymorphism in Java. I have replicated same behaviour in a Poc given below.

1. CommonInterface.java

package com.general;

public interface CommonInterface {

    public void getViews(String str);

    public void getViews(String str, Long id);

}

2. GenericUtility.java

package com.general;

public class GenericUtility implements  CommonInterface{

    @Override
    public void getViews(String str) {
        System.out.println("This is getViews (single param)from  GenericUtility");
        getViews(str, null);
    }

    @Override
    public void getViews(String str, Long id)  {
        System.out.println("This is getViews (multi-param) from  GenericUtility");
    }

}

3. CustomUtility.java

package com.general;

public class CustomUtility extends GenericUtility {

    @Override
    public void getViews(String str) {
        System.out.println("This is getViews (single param)from  CustomUtility");
        super.getViews(str);
    }

    @Override
    public void getViews(String str, Long id) {
        System.out.println("This is getViews (multi-param) from  CustomUtility");
        super.getViews(str, null);
    }

}

4. Main.java

package com.general;

public class Main {

    public static void main(String[] args) {
        GenericUtility utility = new CustomUtility();
        String str = "Random String";
        utility.getViews(str);
    }
}

After running Main.java , my expected output is

This is getViews (single param)from  CustomUtility
This is getViews (single param)from  GenericUtility
This is getViews (multi-param) from  GenericUtility

But output that I get is

This is getViews (single param)from  CustomUtility
This is getViews (single param)from  GenericUtility
This is getViews (multi-param) from  CustomUtility
This is getViews (multi-param) from  GenericUtility

I am not able to understand why com.general.GenericUtility#getViews(java.lang.String) is calling com.general.CustomUtility#getViews(java.lang.String, java.lang.Long)

Is this expected behaviour and what is it that I don't know ?

Thanks in advance.

1
  • You seem to understand why utility.getViews(str) calls CustomUtility.getViews. Then you should also understand why the call getViews(str, null); (equivalent to this.getViews(str, null);) in GenericUtility will call the implementation in CustomUtility. The same rules apply, after all. Commented May 27, 2021 at 6:26

2 Answers 2

2

Java uses runtime virtual dispatch for calls to instance methods (that is, those that aren't static). That still applies to calls to this; that's how, for example, an abstract class can define an abstract method createResultObject to be implemented by subclasses and use that method in its doProcessing method.

In your case, the object is a CustomUtility, so calls to this.getViews(String, String) are dispatched to CustomUtility, which then uses super internally.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, now I understood how it works. :)
0

When GenericUtility.getViews(String) calls getViews(str, null);, the getViews(String, Long) implementation that will be invoked is CustomUtility.getViews(String, Long).

That's because CustomUtility overrides that method too. Irrespective of where the method is called from (including the superclass), a call to an overridden method will cause the overriding implementation to run. The only thing that can change this is the subclass explicitly invoking the method using super.

So here's roughly what is happening:

  1. main calls utility.getViews(str);, which causes overriding CustomUtility.getViews(String) to run.
  2. CustomUtility.getViews(String) calls super.getViews(str), which causes GenericUtility.getViews(String) to run.
  3. GenericUtility.getViews(String) calls getViews(str, null), which causes the overriding CustomUtility.getViews(String, Long) to run (contrary to what you expected)
  4. CustomUtility.getViews(String, Long) calls super.getViews(str, null), which calls GenericUtility.getViews(String, Long) to run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.