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 ??
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.
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 ???
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.