Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide between an Interface or Base Class for an new implementation?

When it comes to implementation, how should i decide to go for an base type or an Interface ? I tried to work out on few examples but i don't get the complete idea :(

Examples on how and why would be greatly appreciated..

like image 597
Sakthivel Avatar asked Mar 23 '13 06:03

Sakthivel


People also ask

How do you decide whether to use an interface or a class?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

Why should we program to an interface and not to an implementation?

If, as a programmer, you code against the implementation then as soon as it changes your code stops working. So think of the benefits of the interface this way: it hides the things you do not need to know making the object simpler to use. it provides the contract of how the object will behave so you can depend on that.

When should a class implement an interface?

When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. A class uses the implements keyword to implement an interface.

How does interface know which implementation to use?

When you call a method on a variable declared as an interface, Java will look up which method to call in the instance's vtable, which is set when you create the instance based on the class. Thus, it actually calls the implementation definde by the class that that object is an instance of at runtime.


2 Answers

A base class, abstract or not, can contain implemented members. An interface cannot. If all of your implementations are going to perform similarly, a base class might be the way to go because all of your child classes can share the same implementations of the members on the base class. If they aren't going to share implementations, then an interface might be the way to go.

Example:

class Person
{
    string Name { get; set; }
}

class Employee : Person
{
    string Company { get; set; }
}

It makes sense for Employee to inherit from Person because the Employee class doesn't have to define a Name property because it shares the implementation.

interface IPolygon
{
    double CalculateArea();
}

class Rectangle : IPolygon
{
    double Width { get; set; }
    double Height { get; set; }

    double CalculateArea()
    {
        return this.Width * this.Height;
    }
}

class Triangle : IPolygon
{
    double Base { get; set; }
    double Height { get; set; }

    double CalculateArea()
    {
        return 0.5 * this.Base * this.Height;
    }
}

Because Rectangle and Triangle have such differing implementations of CalculateArea, it doesn't make sense for them to inherit from a base class.

If you make a base class, and find that in only contains abstract members, you may as well just use an interface.

And, as j__m states, you cannot inherit from multiple base classes, but you can implement multiple interfaces.

I usually define interfaces first, and if I find myself duplicating code in my implementations, I create a base class that implements the interface, and make my implementations inherit from it.

like image 104
Dan Avatar answered Oct 23 '22 05:10

Dan


To decide whether to use an abstract class or an interface, I find this article very helpful Source:

A good way to distinguish between a case for the one or the other for me has always been the following:

  1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class) Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

  2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs. Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.

Please also look at this Interface vs Base class question.

like image 22
Alina B. Avatar answered Oct 23 '22 05:10

Alina B.