Interface, Abstract, or just virtual methods?

I have many systems, let's call them A, B, C, D, E, F, G, H, I, J

All of them have similar methods and properties. Some of them contain the same method and properties, some may vary slightly, and some may vary greatly. Right now I have a lot of duplicate code for each system. For example, I have a method called GetPropertyInformation() that is defined for each system. I'm trying to figure out which method would be the best approach to reduce duplicate code, or maybe one of the methods below is not the way to go:

Interface

 public Interface ISystem { public void GetPropertyInformation(); //Other methods to implement } public class A : ISystem { public void GetPropertyInformation() { //Code here } } 

Abstract

 public abstract class System { public virtual void GetPropertyInformation() { //Standard Code here } } public class B : System { public override void GetPropertyInformation() { //B specific code here } } 

Virtual Methods in the Super Base Class

 public class System { public virtual void GetPropertyInformation() { //System Code } } public class C : System { public override void GetPropertyInformation() { //C Code } } 

One question, although it may be silly, suggests that I went with an abstract approach and I wanted to override GetPropertyInformation , but I needed to pass an additional parameter to it, is this possible or will I need to create another method in an abstract class? For example, GetPropertyInformation(x)

+8
c # oop interface abstract-class virtual-method
source share
6 answers

The approaches with the abstract and the “super base class” are not too different. You should always make abstract abstract classes, and you can provide a default implementation (virtual methods) or not (abstract methods). The decisive factor is whether you ever want to have instances of the base class, I think not.

So this is between the base class and the interface. If there is a strong connection between your classes A, BC, you can use a base class and possibly a common implementation.

If classes A, B, C naturally do not belong to one "family", then use the interface.

And System not such a good name.

And you cannot change the parameter list when overriding. Maybe the default options may help, otherwise you just need 2 overloads for GetPropertyInformation ().

+6
source share

Usually you choose object inheritance when you want to split the implementation and reduce what would otherwise be duplication. Otherwise, the interfaces benefit because they are more flexible since there is no need for a common base class.

As for overriding the method and changing the parameter list, which is simply not possible. Imagine how you would call this method a base class or an interface reference?

+3
source share

I would go with something like what I added below. You still use the interface and collaborative implementation.

 public Interface ISystem { public void GetPropertyInformation(); //Other methods to implement } public abstract class System : ISystem { public virtual void GetPropertyInformation() { //Standard Code here } } public class B : System { public string ExtendedSystemProp {get;set;} public override void GetPropertyInformation() { base.GetPropertyInformation(); var prop = "some extra calculating"; GetExtraPropertyInformation(prop); } public void GetExtraPropertyInformation(string prop) { ExtendedSystemProp = prop; } } ISystem genericSystem = new B(); genericSystem.GetPropertyInformation(); (genericSystem as B).ExtendedSystemProp = "value"; 
+3
source share

You cannot pass an extra parameter to an override. When you override, you override the method with the exact signature. I would suggest that you pass an interface parameter, such as IPropertyInformation , which can be changed for each implementation.

The decision to go with a base class or interface for your implementation really depends on your use. Does AI enough in common with each other that they should really be derived from the same base class? If so, use a base class. Is it really just GetPropertyInformation is common, and otherwise the systems are completely functionally different? Then you really just want them to share the interface.

+2
source share

Others looked at what was originally in my answer, but about the point of “adding a parameter”: don't forget that the last C # also allows you to have optional parameters in methods.

+2
source share

Unless you have a good reason, I would go with an interface. Public virtual methods, although many have been done, are not ideal .

+2
source share

All Articles