aspose file tools*
The moose likes Beginning Java and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Hi. Can an interface contain non-abstract methods. I know it can only contain final, static (constants) variables, but I'm not so sure on the methods it can contain. Can someone help me out on this?
Thanks. Gaia.
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
I suppose you could do that, but why would you want to? Your class implementing the interface would have to be abstract, and then you would have to subclass it to actually create an implementation of the abstract method. I can't think of a reasom why you would want to do this in practice.
Something like this:
interface A {public abstract method1()
The class declaration here needs to be abstract because it contains all the method signatures of the interface it implements, one of which is abstract
abstract class B implements A {public abstract method1();}
Then you would have to subclass it B to have a concrete implementation of method1
class C extends B {public method1{System.out.println("Hey"));}
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
From the JLS 9.4 Abstract Method Declarations

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.


"JavaRanch, where the deer and the Certified play" - David O'Meara
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Originally posted by Gaia Nathan:
Hi. Can an interface contain non-abstract methods. I know it can only contain final, static (constants) variables, but I'm not so sure on the methods it can contain. Can someone help me out on this?
Thanks. Gaia.


Nope.
All methods in an interface are 'abstract' by default although they don't use the abstract keyword.

[This message has been edited by John Bateman (edited August 30, 2001).]


SOURCE CODE should be SURROUNDED by "code" tags.
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
I stand doubly corrected.
I read your question as "can an interface contain abstract methods" and I still didn't answer it right. If that was the question, you could explicitly put the abstract modifier in front of a method in an interface, but the other stuff I said about having to make it abstract in the implementing class if way off. Hope I didn't confuse you.
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Thanks to all your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces