I have a bunch of systems, lets call them A, B, C, D, E, F, G, H, I, J
.
They all have similar methods and properties. Some contain the exact same method and properties, some may vary slightly and some may vary a lot. Right now, I have a lot of duplicated code for each system. For example, I have a method called GetPropertyInformation()
that is defined for each system. I am 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 a 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 stupid, is let's assume I went with the abstract approach and I wanted to override the GetPropertyInformation
, but I needed to pass it an extra parameter, is this possible or would I have to create another method in the abstract class? For example, GetPropertyInformation(x)
The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
All methods in an interface are abstract. This statement is True. It is mandatory for an interface to have abstract methods only to apply multiple inheritance.
An abstract method is implicitly a virtual method. Abstract method declarations are only permitted in abstract classes. public abstract void MyMethod(); The implementation is provided by a method override, which is a member of a non-abstract class.
That's all about whether you can specify non-abstract methods on an interface in Java or not. Yes, it is not possible in the earlier version of Java e.g. until JDK 7 but from JDK 8 onwards you can specify non-abstract methods in form of default and static methods on the interface.
Your abstract and 'super base class' approaches are not too different. You should always make the base class abstract, and you can provide a default implementation (virtual methods) or not (abstract methods). The deciding factor is whether you ever want to have instances of the base class, I think not.
So it's between base class and interface. If there is a strong coupling between your A, B C classes then you can use a base class and probably a common implementation.
If the A, B, C classes do not naturally belong to a single 'family' then use an interface.
And System
is not such a good name.
And you cannot change the parameterlist when overriding. Maybe default parameters can help, otherwise you just need 2 overloads for GetPropertyInformation().
Generally you choose object inheritance when you want to share implementation and reduce what would otherwise be duplication. Otherwise interfaces win because they are more flexible since there is no need for a common base class.
As for overriding a method and modifying the parameter list that's just not possible. Imagine how you would call that method on a base class or an interface reference?
I'd go with something like what I've added below. You still get the benefit of the interface contract and shared 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";
You cannot pass the extra parameter in the override. When you are overriding, you are overriding the method with the exact signature. I would suggest you pass in an interface parameter like IPropertyInformation
that can change per implementation.
The decision to go with a base class or an interface for your implementation really depends upon your use. Do A-I
have enough in common with each other that they should really all derive from the same base class? If so, then use a base class. Is it really that just GetPropertyInformation
is shared and otherwise the systems are totally functionally different? Then you really just want them to share an interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With