*
The moose likes Beginning Java and the fly likes Importing 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 "Importing interfaces ....!!" Watch "Importing interfaces ....!!" New topic
Author

Importing interfaces ....!!

vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Hey guys as far as i have learnt interface is a class with all abstract methods i.e contains methods without any body ...Then how can we directly implement them in our program by just importing them from standard java API ??


"Known is a drop , unknown is an ocean " !
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure what you're asking.

Interfaces are implemented by writing the methods the interface defines. Some interfaces, however, have no methods, and are just "marker" interfaces.
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

I meant how can we directly implement the methods of interfaces by importing them ??? Methods of interface dont have body right ???
better late than never
Greenhorn

Joined: Oct 04, 2009
Posts: 1
[DELETED]


[HENRY: Hijack question deleted. Please create a new topic for new questions.]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

vamsi acharya wrote:I meant how can we directly implement the methods of interfaces by importing them ??? Methods of interface dont have body right ???


You don't. Or more correctly, it doesn't.

Importing an interface (or class) just brings the definition into scope, so you can use it. In this case, so you can declare your class as "implements aInterface" (without using the fully qualified name) and the compiler will know what you are talking about.

It doesn't mean that the interface is implemented. You actually need to actually implement the methods of the interface (or declare your class as abstract), or the compiler will complain.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Thank you henry but am still unclear about something ...After importing and implementing an interface we override the methods of an interface in our program...since the methods of an interface are abstract how can we directly override them without specifying the body ???
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think you're trying to make this more complicated than it is.

An interface defines methods that must be implemented by classes that implement that interface (except for marker interfaces, which don't define any methods). That's it. You're not overriding the method (although in JDK6 you can use the @Override annotation, but it's different than replacing a method of a superclass).

Interfaces are abstract in the sense that they can't be instantiated (and even this is a little blurry if you consider anonymous classes). That doesn't mean their methods can't be written in a class that implements that interface.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Importing interfaces ....!!