Two Laptop Bag
The moose likes Beginning Java and the fly likes An interface with different method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "An interface with different method parameters" Watch "An interface with different method parameters" New topic
Author

An interface with different method parameters

Cole Johnson
Greenhorn

Joined: Aug 04, 2006
Posts: 4
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

Joined: Jan 17, 2006
Posts: 1295
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
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

Joined: Jul 11, 2001
Posts: 14112
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
Cole Johnson
Greenhorn

Joined: Aug 04, 2006
Posts: 4
Thanks for all of your answers. I felt like I have learned something today!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: An interface with different method parameters
 
Similar Threads
Hierarchies, inheritance, polymorphism and interfaces
who can answer this question ?
Problem on interface
Need help breaking up this code
Hierachies and inheritance