Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

structuring common method

 
D Preethi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Visitor pattern.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or create a class ExtendedA which implements that method and let B, C and D extend ExtendedA.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic