I have 3 types of operation :Add,Multiply and Division
Below are the classes to handle this 3 operation:
public class Add
{
public int Value {get;set;}
public Add(int value)
{
Value=value;
}
}
This below class will be called by above main class:
class Source : Add
{
public Source(int value1)
:base(value1)
{
}
}
class Destination : Add
{
public Destination(int value2)
:base(value2)
{
}
}
I am calling above class like this:
Add addObj1 = new Source(10);
Add addObj2 = new Destination(20);
int c=addObj1.Value + addObj2.Value;
Now i have another class like below:
public class Multiply
{
public int Value {get;set;}
public Multiply(int value)
{
Value=value;
}
}
class Source1 : Multiply
{
public Source(int value1)
:base(value)
{
}
}
class Destination1 : Multiply
{
public Destination1 (int value2)
:base(int value2)
{
}
}
Now when i am trying to call this class like this:
Multiply multiplyObj1 = new Source(10); //This is always referring to Source of Add class
Multiply multiplyObj2 = new Destination(5); //This is always referring to Destination of Add class
Now when i rename Source and Destination with Source1 and Destination1 and call like this then it is working:
Multiply multiplyObj1 = new Source1(10); //Working
Multiply multiplyObj2 = new Destination1(5); // Working
int c= multipleObj1.Value * multiplyObj2.Value;
Note:Right now i have created 4 class in which Source and Destination will handle Add class and Source1 and destination1 will handle Multiply
I am talking about how to reuse 2 class for both Add and Multiply class or for other class too(For Division etc..).
Now if i want to perform Division then i have again create 2 more class to handle division.
So here i dont want to duplicate Source and Destination i.e instead of creating 4 class i.e Source,Destination,Source1 and Destination1 is it possible to create only 2 generic class i.e Source and Destination that will perfectly handle both Add and Multiple???