Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Creating a class by implementing Interface

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an application, which peovides interfaces to create java classes (called DataSource)
there is a statement in documentation

Datasource implementations must implement at least one of these interfaces to be recognized by the Datasource framework


I am wondering , "what difference does it make to JVM , if this interface is implemented OR not ?" Wheer does this check happen ? If I am including all methods of an interface in a java class without implementing an interface, will that work ? If not, then how does JVM know that an interface has to be implemented ?
As long as all methods are included in a java class, what difference does it make if interface is implemented or not ?

Thanks
 
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The framework code is expecting the object to be of a class that is an implementation of the interface. When it's not, a run-time error will occur. The exact nature of the error will depends on how the class is used within the framework.

Or, it may not make it that far if your code compiles against an API that expects the interface to be implemented. You'll get a compile-time error in that case.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am wondering , "what difference does it make to JVM , if this interface is implemented OR not ?" Wheer does this check happen ? If I am including all methods of an interface in a java class without implementing an interface, will that work ? If not, then how does JVM know that an interface has to be implemented ?

As long as all methods are included in a java class, what difference does it make if interface is implemented or not ?



What you are describing is often called duck typing, and some languages do work like that. Python and C++ templates are the ones that immediately spring to mind.

Java on the other hand is strongly typed. You have to declare specific types for variables that you use. If you tell us what the framework is we can tell you what would happen, but Bear has given you the possibilities.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!