File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes where to add new method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where to add new method?" Watch "where to add new method?" New topic

where to add new method?

Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
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?
marc weber

Joined: Aug 31, 2004
Posts: 11343

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?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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:

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: where to add new method?
It's not a secret anymore!