- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we synchronize abstract methods in Java?
An abstract method is the one that does not have a body. It contains only a method signature with a semi colon and, an abstract keyword before it.
public abstract myMethod();
To use an abstract method, you need to inherit it by extending its class and provide implementation (body) to it. If a class contains at least one abstract method, you must declare it abstract.
Example
import java.io.IOException; abstract class MyClass { public abstract void display(); } public class AbstractClassExample extends MyClass{ public void display(){ System.out.println("subclass implementation of the display method"); } public static void main(String args[]) { new AbstractClassExample().display(); } }
Output
subclass implementation of the display method
Synchronization − If a process has multiple threads running independently at the same time (multi-threading) and if all of them trying to access the same resource an issue occurs.
To resolve this, Java provides synchronized blocks/ synchronized methods. If you define a resource (variable/object/array) inside a synchronized block or a synchronized method, if one thread is using/accessing it, other threads are not allowed to access.
synchronized (Lock1) { System.out.println("Thread 1: Holding lock 1..."); }
Synchronizing abstract methods
No, you can’t synchronize abstract methods in Java. When you synchronize a method that implies that you are synchronizing the code in it, i.e. when one thread is accessing the code of a synchronized method no other thread is allowed to access it. So synchronizing abstract methods doesn’t make sense, if you still try to do so a compile-time error will be generated.
Example
import java.io.IOException; public abstract class MyClass { public abstract synchronized void display(); }
Output
Compile-time error −
MyClass.java:3: error: illegal combination of modifiers: abstract and synchronized public abstract synchronized void display(); ^ 1 error
- Related Articles
- Can we define an abstract class with no abstract methods in Java?
- Can we synchronize a run() method in Java?
- what are abstract methods in Java?
- Can we define an abstract class without abstract method in java?\n\n
- Can the abstract methods of an interface throw an exception in java?
- Can we override final methods in Java?
- Can we override default methods in Java?
- Can we create an object of an abstract class in Java?
- Can we create an object for the abstract class in java?
- Can we declare an abstract method final or static in java?
- Can we override private methods in Java\n
- Can we define a parameterized constructor in an abstract class in Java?
- What is the difference between non-static methods and abstract methods in Java?
- Can we overload methods of an interface in Java?
- Can we call methods using this keyword in java?
