• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Interfaces and implementation

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You are implementing the method.

Here is a Java Tutorial that describes what an interface is ... Sun Tutorial
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...I wiil go with Jim......
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic