Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic concept problem?

 
Sashi Gundoji
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo all,

I need one basic information about below program. I would like to know is this really works.

-> Base class
public abstract class test {

public Synchronized abstract void readData();


}

-> Child class1

public class test1 extends test {

public Synchronized abstract void readData() {
read state1;
read state2;
}
}

-> Child class2

public class test2 extends test {

public Synchronized abstract void readData() {
read state3;
read state4;
}

}
If this way possible, then i would like to know is that readData method will be Synchronized. If one Daemon is calling child1 readData Method and another daemon is calling child2 readData simultaneously, then what will happen.

thanking you

Regards,
Sashi
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code wont even compile. A method cannot be defined as 'abstract' and 'synchronized' at the same time, because abstract means that the method doesn't posses a body, while synchronizing is related to method implementation.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are all sorts of reasons why the above won't compile, but I think what he's really after is something about the different child classes which each have a method, synchronized or not, that override a common synchronized method (it wouldn't need to be abstract.)

The short answer is that inheritance has nothing to do with it. Each individual object has its own "lock" for synchronization. Two threads will be able to call readData() on any two distinct objects simultaneously with no interference, whether those two objects belong to the same class or to two different classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic