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!
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.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
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.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
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".)
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
posted 12 years ago
Thanks for all of your answers. I felt like I have learned something today!