I have a class which is called MyClass1. I use MyClassFactory1.CreateMyClass() to get an object of MyClass1.
MyClass1 test = MyClassFactory1.CreateMyClass()
This is working. Now i need to change the code because i want to add some features. I want to copy the class MyClass1 in order to create a new class called MyClass2. So MyClass2 should contain the same methods and so on but i want to change the content of a few methods in MyClass2.
For example :
public class MyClass1
{
public void SomeMethod()
{
a = 5;
}
}
public class MyClass2
{
public void SomeMethod()
{
a = 5;
CallAnotherFunctionSomeWhereElse();
}
}
The goal is to create an instance of either MyClass1 or MyClass2 but the output needs to be a MyClass1 because it is used in many other places in the code. test needs to behave like MyClass1.
if(true)
MyClass1 test = MyClassFactory1.CreateMyClass();
else if(false)
MyClass1 test = MyClassFactory2.CreateMyClass();
CallSomeFunctions(test);
The method CallSomeFunctions(MyClass1 input){} can not be changed.
Would it help to derive MyClass2 from MyClass1? I think not because i will not be able to use the changed functionality right? Would it help if i use an interface? I thought about something like this:
IMyClass1
{
void SomeMethod();
}
public class MyClass2 : IMyClass1
public void SomeMethod()
{
a = 5;
CallAnotherFunctionSomeWhereElse();
}
What would be the best approach for this?
Either use inheritance + virtual methods, or an interface. Or a combination of both. This is all basic OOP, I suggest you read some tutorials or buy a good introductory book.