• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a class by implementing Interface  RSS feed

 
nirjari patel
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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
Mike. J. Thompson
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!