• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface question

 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
If i have an interface which defines a method which subclasses must implement can the method be overloaded in the subclass. For example an interface defines a method :
public String getPersonalDetails( String name ){};
in a class that implements this interface can i also have a method such as :
public String getPersonalDetails(string firstname, String Surname){
....
}
in the class implementing the interface?
Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure - give it a try; it works.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it work?
you have tried it,Jim Yingst?
I feel a method in the class which implements a interface can just OVERRIDING the method in the implemented interface instead of OVERLOADING it.
Am I right?I got confused
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it work?
As I said: give it a try; it works. Of course you have to correct a few errors in the code given, like replacing "string" with "String", and an interface declaration can't have a method body (so delete the "{}"). Note also that the original poster asked "can i also have a method such as..." - the also implies he/she wants to actually implement the interface by overriding the required method, and also create an overloaded method. Which is perfectly legal.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clear illustration,Jim Yingst.
I made a misunderstanding!
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply you were right, i do want to implement the interface method as well as another but out of curiosity, would it also be possible to implement an interface and provide an implementation for a method which had a different signature?
THanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No - in order to implement a method from an interface, you must match the signature. You could provide another method which overloads this if you like, which has a different signature. But that has nothing to do with the interface, which requires a specific signature.
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim
THanks, thats just the answer i was looking for
Kola
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic