• Post Reply Bookmark Topic Watch Topic
  • New Topic

If class method accepts interface type parameter, why does the class have to implement the interface  RSS feed

 
Ted Schrey
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the reason I ask is because in an exercise to use an interface type variable as parameter into a method, I did not implement the interface in the class which has the method. The parameters were objects of two other classes which DID implement the interface. It ran fine and I got the result expected, but when I submitted for tests (of correct implementation), I was first told to implement the method of the Interface (not used), and when that done, was next instructed to implement the interface. Ran fine, passed tests, but the result was the same answer that I got w/o implementing the interface. Can anyone tell me why?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class with the method requiring parameters of an Interface type does not need to implement the Interface. Only the class of the object that is being passed as the parameter needs to implement the Interface*.

When you say you were told that the class with the method needs to implement the interface, how were you told this? Was it feedback from your teacher? Perhaps one of your instructions was that the class with the method needed to implement the interface?

*Well the class of the object passed as a parameter needs to be assignable to the Interface type, so it would be ok if a superclass implemented the Interface too.
 
Ted Schrey
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike. I'm just taking an online Java course from of all places, the University of Helsinski to get practice with actually writing code (at http://mooc.fi/courses/2013/programming-part-1/). The way it works is you submit your code and they have a bunch of automated tests that tell you if you wrote proper code, and once these tests are passed, then you submit to their server for final proof. The tests returned that I failed to implement the required method of the interface, and then after I put in the method, that I failed to implement the interface. I didn't and don't see why this was needed, since when I ran the program, it ran fine, but I want to make sure I understand the Interface thing.. Your confirmation that it should not be needed in the class that has the method with interface type parameter helps lessen confusion. Thanks again!

I do have another interface as parameter type question, but I'll post it separately, since it is a slightly different topic...
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It definitely sounds like it was just a specific part of the instructions you were given.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!