File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Bateman
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
BJ Grau
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic