can overloaded methods be synchronized?

Yes, overloaded methods can be synchronized in java.

Note: But you will get mutual exclusive problem if all methods are synchronized and you are using same object to invoke them. So, instead of using synchronized keyword at method level, try to follow below approach.

class LoggerTest {
    private final Object LOCK = new Object();
 
    public void logTest(String s) {
        synchronized (LOCK) {
           //logger statements
        }
    }
 
    public void logTest(int i) {
        synchronized (LOCK) {
           //logger statements
        }
    }
 }

Java interview questions on method overloading and overriding

Please follow and like us:
Content Protection by DMCA.com