• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Creating a class by implementing Interface

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic