Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

An interface with different method parameters  RSS feed

 
Cole Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to get my head around interfaces. for an method in an interface declaration, do you have to declare method parameters, or can they be left up to the class that implements the interface? For example

in this code, I have declared an interface without any parameters in the method declaration. Then when I go to implement the interface in the following two classes, I want to know if there is a way to declare multiple parameters in the method definition. In this case it make no sense to overload the method, since I would have multiple definitions of setSize() for each class. Or, am I using interfaces in the wrong way here? Thanks for your help!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classes implemenitng an interface must match the method signiture of the interface exactly including number and order of parameters. It also may throw only those checked Exceptions that are decalred in the throws clause of the interface method declaration.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cole Johnson:
...for an method in an interface declaration, do you have to declare method parameters, or can they be left up to the class that implements the interface? ...

You need to specify the method arguments in the interface, since these are part of the method signature.

If you write a method in an implementing class that takes different arguments, then you're actually overloading -- not overridding. And if you do not override an interface's abstract method, then the implementing class itself must be declared abstract. This is why your sample code won't compile.

One solution to your situation is for the method to take a single argument that is an array of dimensions.
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure you already know this code won't compile. (Not just because of the funny typo in the Cylinder setSize method declaration.)

An interface defines a contract; an implementing class has to override all the methods of an interface. You are overloading the setSize method in your code because you are changing the parameters.

Read the pages below on interfaces.

Sun Java Interface Tutorial

WikiPedia Java Interfaces
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, methods with different parameter lists are always totally different methods, no matter whether their name is the same. (That's what is meant when we say that "the paramter list is part of the method signature".)
 
Cole Johnson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your answers. I felt like I have learned something today!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!