Notes:
- Besides the logical differences, I am also interested to know about the technical differences as reflected in C# specifically (hence, it is not a question for Programmers).
- This question is a bit similar, but it asks about methods while I am asking about classes, so it is not a duplicate.
Circle
and Rectangular
are shapes. Both have a perimeter and an area, but different implementations for calculating them. I can see three different way to implement such a logic, and I am not sure what is the difference between these approaches.
Using dynamic polymorphism:
class Shape
{
public virtual double Perimeter() { /* logic */ }
public virtual double Area() { /* logic */ }
}
class Rectangular : Shape
{
public override double Perimeter() { /* logic */ }
public override double Area() { /* logic */ }
}
class Circle : Shape
{
public override double Perimeter() { /* logic */ }
public override double Area() { /* logic */ }
}
Using an abstract class:
abstract class Shape
{
public abstract double Perimeter() {}
public abstract double Area() {}
}
class Rectangular : Shape
{
public override double Perimeter() { /* logic */ }
public override double Area() { /* logic */ }
}
class Circle: Shape
{
public override double Perimeter() { /* logic */ }
public override double Area() { /* logic */ }
}
Using an interface:
interface IShape
{
double Perimeter();
double Area();
}
class Rectangular : IShape
{
public double Perimeter() { /* logic */ }
public double Area() { /* logic */ }
}
class Circle: IShape
{
public double Perimeter() { /* logic */ }
public double Area() { /* logic */ }
}
In the title, I mentioned that I am interested in an answer from the perspective of OOP. I want to understand the theoretical differences between the approaches, based on the OOP paradigm, and from that - understand the technical differences. For example, I know that interfaces methods cannot have an implementation while virtual methods can, but I do not understand why it consists with the OOP paradigm.
Please answer with all the theoretical differences and for each difference, the derived technical difference in C#.
Many thanks.
Edit: @AdrianoRepetti and @Biscuits say that my question is vague. My English is not great, so I will try to explain myself as clearly as I can.
I showed three different ways of doing the same thing. But is it really the same thing? What are the differences between them from a program architecture POV? I mean, when I design the program, why should I choose one over the other? What are the essential differences, and how those differences are expressed in the syntax of C#? I hope my question is clearer now.
If someone who speaks good English think he/she understand my question and can edit it to be clearer and grammarly correct, I will be grateful.
Thanks a lot!