I’m aware of the concept of abstraction in programming and the use of interfaces and abstract classes but am a little confused with how it’s actually achieved and where the abstraction actually occurs. Consider the following basic example with a C# interface:
interface MyInterface
{
int Add(int num1, int num2);
}
public class MyClass : MyInterface
{
public int Add (int num1, int num2)
{
return num1 + num2;
}
}
public class Program
{
public static void Main (string[] arg)
{
MyClass MyObject = new MyClass();
Console.WriteLine(MyObject.Add(1,2));
}
}
Where is the abstraction occurring? For me, the programmer, there appears to be none. There appears to be no hiding of implementation details. I can simply instantiate a new MyClass object with a MyClass reference and interact with its methods directly – how does the interface abstract MyClass and its methods?
Perhaps if the “Add” function itself executes another method from another class, then that particular method and class would be hidden and abstracted away? But I don’t see why an interface or abstract class is needed to facilitate this.
I’m guessing the abstraction via an interface must occur when I’ve created my program, say as some kind of library, and another user wants to use my class library in their code?
Would be very grateful for some clarification!