Why can't an abstract method be synchronized?

前端 未结 4 1040
深忆病人
深忆病人 2020-12-13 00:45

I was reading a thread from CodeRanch saying that abstract methods could not be synchronized due to the fact that an abstract class cannot be instantiated, meaning no object

4条回答
  •  时光取名叫无心
    2020-12-13 01:34

    synchronized void foo()
    {
        body
    }
    

    is defined to be equivalent to

    void foo()
    { 
        synchronized(this)
        {
            body
        }
    }
    

    (if static, synchronized on the class instead of this)

    Since an abstract method has no body, synchronized keyword on the method is undefined.

提交回复
热议问题