This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Basic Question about extending/implementing" Watch "Basic Question about extending/implementing" New topic
Author

Basic Question about extending/implementing

Fernando Martinez
Greenhorn

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,
Fernando

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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
Bartender

Joined: Oct 02, 2003
Posts: 11439
    
  16

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
 
Don't get me started about those stupid light bulbs.
 
subject: Basic Question about extending/implementing