deleyd
asked on
Java refactoring: removing duplicate code
In the following scenario I have:
interface IClassA
abstract class ClassA implements IClassA
concrete classes ClassB1 and ClassB2 extend ClassA
ClassB1 and ClassB2 both have methods DoStuff()
The code for DoStuff() is identical in both ClassB1 and ClassB2
Is it possible to refactor this to get rid of the identical code?
interface IClassA
abstract class ClassA implements IClassA
concrete classes ClassB1 and ClassB2 extend ClassA
ClassB1 and ClassB2 both have methods DoStuff()
The code for DoStuff() is identical in both ClassB1 and ClassB2
Is it possible to refactor this to get rid of the identical code?
public interface IClassA {
public int DoStuff();
public int ClassSpecific();
}
public abstract class ClassA implements IClassA {
//public ClassA(){}
public void aCommonMethod() {
}
}
public class ClassB1 extends ClassA {
public ClassB1(){}
@Override
public int DoStuff()
{
return ClassSpecific();
}
@Override
public int ClassSpecific()
{
return 9;
}
}
public class ClassB2 extends ClassA {
public ClassB2(){}
@Override
public int DoStuff()
{
return ClassSpecific();
}
@Override
public int ClassSpecific()
{
return 5;
}
}
(Imagine DoStuff() is a lot more complicated than it is here, but it does have buried in it a call to ClassSpecific().)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Make the methods static in the abstract class then.
ASKER
I get:
"Cannot make a static reference to the non-static method ClassSpecific() from type IClassA."
"Cannot make a static reference to the non-static method ClassSpecific() from type IClassA."
In that case I think you'll want to define
asFahrenheit, asCelsius, asKelvin
in the interface/abstract class as well.
You'll need a common "something" (abstract class/interface) for the shared code to be able to call common methods like "asCelsius" and have them execute the right specific implementation.
As you say, since the code is identical this should be solvable with the right hierarchy. But the interface (or abstract class) will need to include all of the methods that are shared between the two concrete classes.
Doug
asFahrenheit, asCelsius, asKelvin
in the interface/abstract class as well.
You'll need a common "something" (abstract class/interface) for the shared code to be able to call common methods like "asCelsius" and have them execute the right specific implementation.
As you say, since the code is identical this should be solvable with the right hierarchy. But the interface (or abstract class) will need to include all of the methods that are shared between the two concrete classes.
Doug
ASKER
I was missing the keyword 'abstract' !
I had public class AbstractClass
Now it works great once I noticed that. Thank you!
I had public class AbstractClass
Now it works great once I noticed that. Thank you!
Well done Doug! ;)
ASKER
Open in new window
methods asFahrenheit(), asCelsius(), asKelvin() are the Class Specific methods.Moving the code to the abstract superclass (ClassA), it complains, because the superclass doesn't define asFahrenheit, asCelsius, asKelvin.