This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes structuring common method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "structuring common method " Watch "structuring common method " New topic
Author

structuring common method

D Preethi
Ranch Hand

Joined: May 24, 2008
Posts: 68
Hi All,

I have the following scenario:

There are classes "B","C","D" which are extending class "A". I do not have access to class "A" here. Now, I need to introduce a method "m" which can be commonly used by "B", "C" and "D". Can anyone suggest me some design pattern which can help me achieve this without having to duplicate the method across "B", "C " and "D" ?


Thanks,
Preethi
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Have a look at the Visitor pattern.


apigee, a better way to API!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or create a class ExtendedA which implements that method and let B, C and D extend ExtendedA.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Or create an interface with method m and let those three classes implement that interface. You'll need to use references of that interface type to call the method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: structuring common method
 
Similar Threads
accessing subclass name in superclass
Question
Logic with a looping method
anonymous class question
Abt Inner Classes....