Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces and implementation

 
Ken Barsi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you implement and interface and you implement all of the methods, is this the same as overriding the methods even if you don't do anything to the invoked method?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Implementing methods (of an interface) is unrelated to overriding methods (of a superclass). With an interface, there is nothing there to override, because there is nothing (i.e., no method body) there at all.
 
Ken Barsi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to make sure I understand this. An interface can contain methods. If I have a new class that implements an interface I must implement all the interfaces methods in my new class.

public interface Worker{{
public void work()
}

public class Test implements Worker{
public void work(){
System.out.print("This class implements the work method");
}
}

This looks to me to be overriding the work() method, but you say the work() method has no body so there is nothing to override. So what have I done if not overriding? Did I implement the method if not overriding?

Thank you for your response.
 
Mike Mc Afee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You are implementing the method.

Here is a Java Tutorial that describes what an interface is ... Sun Tutorial
 
Bhavik patel
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but Mike..look at this.....
--------------------------------
interface i{

public void s();
}

class c implements i
{

}
----------------------------------
when i compile the class c, it shows this error.

c is not absract and does not override abstract method s() in i
----------------------------------
if i go with you then the error should be like this...

c is not absract and does not implement abstract method s() in i
[ February 26, 2007: Message edited by: Bhavik patel ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bhavik - strictly speaking, the error message that the compiler gives you is wrong.

Sun's Java compiler isn't bugfree, just like any piece of software...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree with most of the responses above. Implementing a method is a particular type of overriding, at least according to the definition used in the JLS. And let's not talk about classes vs. interfaces - it's more accurate here to talk about abstract methods and concrete methods. The definition of overriding does not depend on whether methods are abstract or concrete, but the definition of implementing does. It's possible for a concrete method to override another concrete method, or to override an abstract method. It's also possible for an abstract method to override another abstract method, or even to override a concrete method. All four of these cases are considered overriding, by definition - but only the second case is also (more specifically) called implementing.

So, for Ken's original question, the simple answer is "yes".
[ February 26, 2007: Message edited by: Jim Yingst ]
 
Michael Battersby
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jim. Abstract methods are 'overridden' when you implement them. Therefore, by implementing an interface's methods, you are actually overriding them.
 
Bhavik patel
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...I wiil go with Jim......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic