• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Question about extending/implementing

 
Fernando Martinez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic