• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Importing interfaces ....!!

 
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[DELETED]


[HENRY: Hijack question deleted. Please create a new topic for new questions.]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic