Suppose there are 2 classes, Parent class and Child class which extends Parent class. Now and both have two method which are synchronized.My question which object lock will be use for locking this synchronized methods i create object like : Parent p=new Child(); Is is parent object lock or Child object lock?
3 Answers
I think the reason people get confused here is that: class inheritance inherits instance fields. But for methods, it will just inherits methods implementation. So there is only lock being used, which is p in your case. Let me know if it makes sense.
Also it is not a good idea to use inheritance for additional locking. Use composition - decorator pattern. See JCIP Chapter 4.4.1