Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Importing interfaces ....!!

 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[DELETED]


[HENRY: Hijack question deleted. Please create a new topic for new questions.]
 
Henry Wong
author
Marshal
Pie
Posts: 21003
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic