4

If a class has synchronized methods, does its subclass also have the same synchronized methods, whether simply inherited or overriden by the subclass?

Specifically,

  • the legacy Vector has synchronized methods, and Stack is a subclass of Vector. Does Stack also have synchronized methods?

  • the legacy Hashtable have synchronized methods, and Properties is a subclass of Hashtable. Does Properties also have synchronized methods?

Motivations of my questions are from What are the replacements for legacy collections `Stack` and `Properties`?

Thanks.

  • 2
    Why would the `synchronized` property of a method disappear just because it's inherited? – Kayaman Nov 21 '17 at 18:48
  • Anyone wants to answer my questions on the specific collections `Stack` and `Properties`? –  Nov 21 '17 at 18:53
  • 2
    @Ben there's no point in answering the question for specific collection classes. It's the same answer for all classes. – Andy Turner Nov 21 '17 at 18:54
  • @Andy My point is whether we need to find synchronized replacements for `Stack` and `Properties`. See here https://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated/1386288#comment81795220_1386288 –  Nov 21 '17 at 18:55
  • @Ben How is your first and second question related? If you meant to ask whether `Stack` and `Properties` inherits `synchronized` methods, the answer is yes. If you meant to ask should Java have an alternate thread safe class like `Stack`, then that seems to be an unrelated question in comparison to your first question.. – Chetan Kinger Nov 21 '17 at 19:20
  • @CKing that unrelated question is my motivation. I have updated my post with it. –  Nov 21 '17 at 20:15
  • @Ben The first two questions *"the legacy Vector has synchronized methods, and Stack is a subclass of Vector. Does Stack also have synchronized methods?"* and *"the legacy Hashtable have synchronized methods, and Properties is a subclass of Hashtable. Does Properties also have synchronized methods?*" have been answered in my post. The third question *"Motivations of my questions are from What are the replacements for legacy collections `Stack` and `Properties`?"* is answered in the question you linked to. What part of your question is not answered yet? Please explain clearly! – Chetan Kinger Nov 22 '17 at 06:03

2 Answers2

4

If a class has synchronized methods, does its subclass also have the same synchronized methods, whether simply inherited or overridden by the subclass?

A synchronized method from a super class can be overriden in a subclass as a non synchronized method and vice-versa.

If the subclass does not override a synchronized method but simply inherits it, the method will be inherited as-is as a synchronized method.

I was wondering whether we need to find synchronized replacements for Stack and Properties

Stack and Properties are thread-safe classes in the sense that most of their methods are synchronized (including the inherited methods from Vector/Hashtable). Although, that still doesn't safeguard them from being incorrectly used in a multithreaded context.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
0

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

However, a subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass

So yes, synchronized methods are inherited by subclass. You can go through javadoc for more details.

codingenious
  • 8,385
  • 12
  • 60
  • 90
  • 2
    I have no idea what the first 3/4 of this answer has to do with answering the question. – nickb Nov 21 '17 at 18:51
  • http://gee.cs.oswego.edu/dl/cpj/mechanics.html says synchronised is not inherited. – Veneet Reddy Nov 21 '17 at 18:52
  • Trying to explain inheritance in all. – codingenious Nov 21 '17 at 18:52
  • 4
    @VeneetReddy it says "So the synchronized modifier is not automatically inherited when subclasses override superclass methods", as in if you override the method but omit the `synchronized`, the overridden method isn't synchronized. – Andy Turner Nov 21 '17 at 18:53