File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Basic Question about extending/implementing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Basic Question about extending/implementing" Watch "Basic Question about extending/implementing" New topic

Basic Question about extending/implementing

Fernando Martinez

Joined: Jun 27, 2011
Posts: 12
Why the following code doesnt show any conflict since both, method1 in iface1 and aclass1 have the same name and must be implemented by class test1a. It seems that test1a is implementing the aclass1 method and not the iface1, why?

Thanks in advance,

Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Welcome to the Ranch!

Your class is implementing both. The abstract method / interface method merely indicate that there should be a method called method1 that takes no parameters and returns void. And it does just that.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46340
There is only one way you could have a conflict: if the two methods did not have compatible intentions. I have amended your code to introduce such an incompatibility By the way: please use spaces, not tabs, for indenting.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Note that even in Campbell's example, you can write code that compiles and runs - it just may not work the way everyone hopes.

When you implement an interface, all you are saying is "My class will have a definition/implementation for these methods".

When you extend an abstract class with a concrete class, you have to implement the abstract methods.

Note that by writing the method once, you have fulfilled your obligation to both.

If your mom says "You have to clean your room today", and your dad says "You have to clean your room today", as long as you clean it once, you have fulfilled both obligations.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Basic Question about extending/implementing
It's not a secret anymore!