Results 1 to 5 of 5
- 02-10-2011, 08:27 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
How to eliminate method duplicacy ?
There are three classes X, Y and Z as shown below, where a method "show" in all three classes has same logic.
class X extends BaseClass1{
show(){
----
----
----
}
}
class Y extends BaseClass2{
show(){
----
----
----
}
}
class Z extends BaseClass3{
show(){
----
----
----
}
}
Now, I like to remove,
show(){
----
----
----
}
from Classes X, Y and Z. Would like to reuse the "show" method from common location(i.e either class or interface or from any source).
As the above three classes have already being extended to some BaseClass1/2/3 classes, I lost the idea of inheritance. As, Java
does not support multiple inheritance. I have no idea how to implement the same.
Any idea how to solve this issue w.r.to coding? Can any one help me on this ?
- 02-10-2011, 08:31 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
A static method in another class?
Edit or another class that "BaseClass1/2/3" extend, but the inheritance mecahnism is not really about "code reuse", if you find yourself using it for only that purpose you are doing something wrong.
- 02-10-2011, 08:43 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
I agree, inheritance is not for reusability, but it is also for inheriting the features of parent class.
So I can assume your idea is like this, please correct me if i'm wrong.
Class Temp {
static show {
-----
-----
-----
}
}
Class BaseClass1 extends Temp{
}
Class BaseClass2 extends Temp{
}
Class BaseClass3 extends Temp{
}
And finally,
Class X extends BaseClass1 {
}
so on.....
- 02-10-2011, 08:47 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
If you do it like that (it's perfectly valid) you don't even need to make the show() method static.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-10-2011, 09:40 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 08:20 PM -
i need help with my code.how can i eliminate duplicate number from my sorted list
By yinghang in forum New To JavaReplies: 6Last Post: 04-13-2009, 05:19 AM -
How to eliminate 'reduce to icon' and 'maximize' buttons.
By hendrix79 in forum New To JavaReplies: 2Last Post: 12-30-2008, 03:10 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 08:55 PM -
Avoid/Eliminate Duplicate Code
By kicker in forum New To JavaReplies: 6Last Post: 09-11-2008, 05:14 AM
Bookmarks