Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface, Abstract, or just virtual methods?

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)

like image 566
Xaisoft Avatar asked Sep 28 '11 20:09

Xaisoft


People also ask

Is it better to use interface or abstract class?

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.

Should interface methods be abstract?

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.

Is abstract method a virtual method?

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.

Can we have interface without abstract methods?

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.


4 Answers

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().

like image 197
Henk Holterman Avatar answered Sep 19 '22 23:09

Henk Holterman


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?

like image 32
David Heffernan Avatar answered Sep 23 '22 23:09

David Heffernan


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";
like image 27
scottm Avatar answered Sep 19 '22 23:09

scottm


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.

like image 41
skaz Avatar answered Sep 23 '22 23:09

skaz