File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface method" Watch "Interface method" New topic
Author

Interface method

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Sep 14, 2001
Posts: 582
Thanks Valentin and all of you for your explanation
I understand now ..
thanks again
daniel
Oscar Garcia
Greenhorn

Joined: Mar 05, 2002
Posts: 23
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?


Oscar S. Garcia<br />SCJP<p>--------------------------<br />Pilipino ako...ayos ba?<br />--------------------------
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
Oscar Garcia
Greenhorn

Joined: Mar 05, 2002
Posts: 23
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface method
 
Similar Threads
Interface definition
Does HOME Interface represent FACTORY or ABSTRACT pattern?
NX:Hotel-room booking system
getServletContext() method?
About Comparator Interface