The moose likes Beginning Java and the fly likes Interfaces and implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces and implementation" Watch "Interfaces and implementation" New topic

Interfaces and implementation

Ken Barsi

Joined: Feb 07, 2007
Posts: 2
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

Joined: Mar 22, 2005
Posts: 42956
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

Joined: Feb 07, 2007
Posts: 2
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

Joined: Jan 31, 2007
Posts: 19
Yes. You are implementing the method.

Here is a Java Tutorial that describes what an interface is ... Sun Tutorial
Bhavik patel
Ranch Hand

Joined: Feb 20, 2007
Posts: 49
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 ]

Bhavik Patel
Glassfish Consultancy service At
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14917

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...

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Michael Battersby

Joined: Feb 05, 2007
Posts: 3
I agree with Jim. Abstract methods are 'overridden' when you implement them. Therefore, by implementing an interface's methods, you are actually overriding them.

SCJP 1.4, SCWCD<br /> <br />Always invoke your imagination, it may throw an exception, but you'd be able to handle it.
Bhavik patel
Ranch Hand

Joined: Feb 20, 2007
Posts: 49
ok...I wiil go with Jim......
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Interfaces and implementation
It's not a secret anymore!