• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface method

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I want to ask about method in interface...
Can you help me to explain why we cannot use 'synchronized' to declare method in interface?
thanks
daniel
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because interface methods have no implementation (i.e. they are implicitely abstract) and that synchronization is an implementation issue. it is the job of the implementing classes to make sure that the method may (not) be synchronized. When you declare a method in an interface, you don't really know what its implementation will be, and therefore you can't possibly know why that method should be synchronized or not. Does this make sense?
[ March 05, 2002: Message edited by: Valentin Crettaz ]
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentin,
I'm a little confused. What is the difference between an interface and abstract class? Is there a rule of thumb to follow to decide when to use interface, when to use abstract class?
Thanks a lot,
Jenny
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jenny Yin:
Hi Valentin,
I'm a little confused. What is the difference between an interface and abstract class? Is there a rule of thumb to follow to decide when to use interface, when to use abstract class?
Thanks a lot,
Jenny

An abstract class can contain method definitions, an interface cannot. If you choose to extend an abstract class, you need only to override the methods that are declared abstract within the abstract class. If you extend an interface, since an interface can not supply method definitions, you must provide implementations for all methods declared in the interface in the implementing class.
Remember, an abstract class is not required to have any abstract methods - it simply means you can't make an instance of that class.
Check out the following sections of the JLS:
8.1.1.1 abstract Classes
Interfaces
If you have more questions, please ask.
Corey
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey,
So, if an abstract class only declares abstract methods which don't have definition, it looks just like an interface, right?
Thanks,
Jenny
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The differences between abstract classes and interfaces:
- abstract classes may have member variables while interfaces only have constants.
- you cannot declare private members in interfaces while it is possible to do so in abstract classes.
- abstract classes may declare constructors, interface can't.
[ March 08, 2002: Message edited by: Valentin Crettaz ]
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Valentin and all of you for your explanation
I understand now ..
thanks again
daniel
 
Oscar Garcia
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Valentin!
Earlier, you have said that an abstract class
MAY declare a constructor.
My question is, What's the use of such constructor when you cannot instantiate an abstract class?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concrete subclasses can call it! It can do useful work. Just because a class is abstract doesn't mean EVERY method in the class is abstract. Some methods may do limited things, but they can still get and set instance variables.
In fact, every class *has* to have a constructor. If you don't create one the compiler creates a no-arg constructor for you. So you already have at least a no-arg constructor in every abstract class. Adding more is up to you.
 
Oscar Garcia
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
Concrete subclasses can call it! It can do useful work. Just because a class is abstract doesn't mean EVERY method in the class is abstract. Some methods may do limited things, but they can still get and set instance variables.
In fact, every class *has* to have a constructor. If you don't create one the compiler creates a no-arg constructor for you. So you already have at least a no-arg constructor in every abstract class. Adding more is up to you.

you're right!!! thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic