Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Question about synchronized abstract modifier

Hi!

BTW, why abstract synchronized is not valid? What is the reason for that?

Also abstract static is not allowed as well - that is because of some logical contradiction?


Thank you!

The simplest reason is you can't letting those keywords put together won't help you achieve anything. If you have gone through our threads video, you know the synchronized keyword gets applied to the monitor lock of the current object, which is pretty much of an implementation detail.


Now, if you have a method with synchronized and abstract together, the 'abstract' keyword makes the method a prototype - a method that doesn't have an implementation. That said, the implementation happens at an overriding method in the subclass that extends the given abstract class; and that is the place where you need to decide if and how you need to deal concurrent access. Allowing synchronized to be placed with the abstract keyword at the abstract method would only make it much confusing as it would be unclear where the monitor lock would be applied and the implementation would sound weird as you have implementation details outside the entity where you make your implementation on.


A method cannot have abstract + static together for the reason that it would make it totally unusable. You know - the only purpose of having an abstract method is to make sure it will be overridden in a subclass. You cannot call an abstract method unless it is overridden. Now if you put static together with the declaration of an abstract method, overriding makes no sense because static methods cannot be overridden. (Note: of course you can have a method with the same signature as of in the super class in a sub class, but if the method is static, it would simply 'shadow' it instead of overriding).


On the other hand, a static method would let you call that method without needing to instantiate its surrounding class.


class SomeClass {
  public static void method() {
       // implementation
  }
}


because method() is static, you can simply call it as below:


SomeClass.method();


Now, if method() was abstract at the same time, what would happen when you try to call it like above? In that case, you are trying to directly call an abstract method which doesn't have an implementation. That is going to be contrary to the notion of abstraction!



Hi!

You wrote - "If you have gone through our threads video" - yes, thank you, I 've done!

And were can I find the same videos for Enum and OOP?

Also in the video about Threads reporter recalls to some Templates video - where can I find it?


Thank you!

We don't have a video about "Enum and OOP" uploaded - the closest we have is the enum document, which is a part of the training course:

http://www.examlab.org/lesson/enums.pdf


ExamLab © 2008 - 2024