• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

where to add new method?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an interface say A with method1().
There is 1 abstract class say B which implements this interface A. The abstract class has implemented the method1 with 2 more methods, methodE, methodD.
If I want to add 1more methodH which will do methodE, MethodD and another new one methodZ, what can I do in this case? Should I make a new method in interface or abstract class?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider what will happen when you upcast. That is, if you instantiate an object that extends the abstract class, and then upcast its reference to the interface type, would it make sense for the new method to be available there?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put it on your interface then all implementations of the interface will have to implement it, not just the abstract class you have today.

If you put it on your abstract class then any references that are typed to the interface cannot call it. Does that matter?

Avoid this really ugly situation:
 
reply
    Bookmark Topic Watch Topic
  • New Topic