Reputation: 1296
Given: I have two different projects legacy A and nextGen B. There is the third project C which is shared between A and B.
Objective: I have to do an averageCalculation() in the third project C. But the implementation is different for the projects A and B. Using the same method signature but different implementations, how do I create a design? Note: the project A and B should just call averageCalulation() the same method signature.
Project C
Interface I {
averageCalculation();
}
Class CClass implements I{
?<averageCalculation()-for A>
?<averageCalculation()- for B>
}
Project A
{
I i1 = new CClass();
i1.averageCalculation();
}
Project B
{
I i2 = new CClass();
i2.averageCalculation();
}
Is the above approach correct? if so how would i create two implementations of averageCalculation() in CClass?
Upvotes: 1
Views: 1224
Reputation: 2300
An interface is just a contract for classes that implement it to define. If a class needs two different implementations of a single method in an interface then you should consider redesigning your project
Why do you need CClass ? You can have a class in Project A implement I and another class in Project B doing the same.
EDIT : the compiler will not let you have two different implementations of the method with the same signature. You do have an option to overload it if that is what you want
public interface SomeInter
{
public void doSomething();
}
public class ImplClass implements SomeInter
{
@Override
public void doSomething() {
// TODO Auto-generated method stub
}
public void doSomething(String abc)
{
// TODO Auto-generated method stub
}
}
Hope this helps!!
Upvotes: 0
Reputation: 156748
Create two different classes that implement your interface, and use a different class in each project:
Project C
interface I {
averageCalculation();
}
class CClassForA implements I{
averageCalculation(){...} // for A
}
class CClassForB implements I{
averageCalculation(){...} // for B
}
Project A
{
I i1 = new CClassForA();
i1.averageCalculation();
}
Project B
{
I i2 = new CClassForB();
i2.averageCalculation();
}
Upvotes: 2
Reputation: 477
I am not sure I understand your problem entirely but here is a solution. You can't change the legacy projects but you want to A and B to conform to some Interface I. You can do this by wrapping A and B in something that does conform to I and implement I with A and B's respective implementations.
public class Problem {
public static class A{
public int foo(){
return 3;
}
}
public static class B{
public int foo(){
return 5;
}
}
public interface TheFoo{
public int foo();
}
public static class AWrapper extends A implements TheFoo{
public int foo(){
return super.foo();
}
}
public static class BWrapper extends B implements TheFoo{
public int foo(){
return super.foo();
}
}
public static void main(String[] args){
//TheFoo[] myFoos = new TheFoo[]{new A(), new B()}; Won't work
TheFoo[] myFoos = new TheFoo[]{new AWrapper(), new BWrapper()};
for(TheFoo curFoo : myFoos){
System.out.println(curFoo.foo());
}
}
}
Upvotes: 0