0

I have an abstract class say CTest which contains only the abstract method f1() and nothing else. Similiarly, i have a Interface ITest with the only method f1(). Here both the CTest abstract class and ITest interface does the same thing.

The one difference is that, the Interface provides the flexibility that it can be implemented in any classes which already derived from other class but abstract classes cannot.

Apart from the above difference, What is the actual difference between these two? and which one is efficient here(CTest or ITest)? When i should use what? Any specific scenario's in OO Design and any general suggessions on this are helpful

Mohan Kumar
  • 6,008
  • 6
  • 28
  • 36
  • Are you curious about _efficiency_? In Java, interface method calls internally use the [`invokeinterface`](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.invokeinterface) instruction which is [comparatively expensive](http://stackoverflow.com/a/1505476/591495) to [`invokevirtual`](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.invokevirtual). – obataku Aug 07 '12 at 05:39
  • Look this may it helps you..http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class – Sumit Singh Aug 07 '12 at 05:42

8 Answers8

2

Other than inheritance, it depends on the scenario. Check this code project article with an excellent example.

[From the article]

Lets Assume you need to make three classes, first is CAR, second is MAN, third is WOMAN. Now you need a function in each of them to define how they Move. Now all three can move but CAR moves entirely in different way than MAN and WOMAN. So here we use an Interface IMOVEMENT and declare a function MOVE in it. Now all three classes can inherit this interface. So the classes goes like this.

public interface IMovement
{
    void Move();
}

public class Car : IMovement
{
    public void Move()
    {
        //Provide Implementation
    }
}

public class Man : IMovement
{
    public void Move()
    {
        //Provide Implementation
    }
}

public class Woman : IMovement
{
    public void Move()
    {
        //Provide Implementation
    }
}

But, since MAN and WOMAN walk in similar way, so providing same behavior in two different methods will be code redundancy, in simpler words code is not re-used. So we can now define a Abstract Class for Human Beings movements, so this class can be HUMANBEINGMOVEMENT. Also the same can be applied to CAR class, since there are lot of manufactures for cars and all cars move in similar way so we can also define a abstract class for Cars movement which can be CARSMOVEMENT. So our refactored code will be .

public interface IMovement
{
    void Move();
}

public abstract class CarsMovement : IMovement
{

    public virtual void Move()
    {
        //default behavior for cars movement
    }
}

public class SuzukiCar : CarsMovement
{
    public override void Move()
    {
        //Provide Implementation
    }
}

public abstract class HumanBeingMovement : IMovement
{

    public virtual void Move()
    {
        //default behavior for human being movement
    }
}

public class Man : HumanBeingMovement
{
    public override void Move()
    {
        //Provide Implementation
    }
}

public class Woman : HumanBeingMovement
{
    public override void Move()
    {
        //Provide Implementation
    }
}
ABH
  • 3,391
  • 23
  • 26
  • Those colons are used to replace the "implements" keyword, if anybody is wondering. – edwga Aug 07 '12 at 05:33
  • @hamad: This example explains the usage of interface and abstract class in great way. But in my case, what is the actual diff these two? which one should i use? – Mohan Kumar Aug 07 '12 at 05:46
  • @mhn If there is some common functionality you want to implement for all the derived classes then you should use an abstract class as in f1() in your case. If you make f1 virtual then this can be overridden by the child class as well. On the other hand, interfaces describe a group of related functionalities that can belong to any class or struct. If there is no common functionality then you should use interface as it's generally understood that if there is and abstract class then there will be some virtual methods. – ABH Aug 07 '12 at 07:13
1

In Java prefer Interfaces to Abstract Classes. Refer Item 18 in Effective Java

enter image description here

Main Points :

  • Existing classes can be retroffited to implement a new interface.

  • Interfaces are ideal for defining mixins.

  • Interfaces allow the construction of nonheirarchical type frameworks.

  • Interfaces enable safe, powerful functionality enhancements.

Ajay George
  • 11,759
  • 1
  • 40
  • 48
0

in c# it allows only single level inheritance. therefore interfaces can be use to do multiple inheritances

and also for more details :

http://www.dotnetfunda.com/forums/thread4085-difference-between-interface-and-abstract-class.aspx

http://sadi02.wordpress.com/2008/05/08/what-is-difference-in-an-abstract-class-and-an-interface/

DevT
  • 4,843
  • 16
  • 59
  • 92
  • http://stackoverflow.com/questions/3635589/difference-between-abstract-class-and-interface – DevT Aug 07 '12 at 05:35
0

For me it better to use interface here. Abstract class should be used when you could extract some code there (you could implement method or there is other stuff that want to invoke it).

user854301
  • 5,383
  • 3
  • 28
  • 37
0

In this case, and assuming that your Abstract Class will only contain abstract methods, you should, in my opinion, go with the Interface. Abstract classes with abstract methods and interfaces serve the same purpose, however, you can extend only one class but implement as many as you want, thus making your code less prone to significant changes should you decide the inherit some functionality from some other class.

Regarding your question: But What is the actual difference between these two? and which one is efficient here(CTest or ITest)? When i should use what? Any specific scenario's in OO Design and any general suggessions on this are helpful

Interfaces are similar to contracts, when a class implements an interface, it guarantees an implementation. This is usually helpful when someone wants to provide functionality but does not want to reveal internal code, so the developer will just throw out the interface so that you can make your calls without knowing how is each method implemented. You can obviously implement as many interfaces as you like.

Abstract classes allow you to create a class which has certain behaviours which are specified and some others which are left to be implemented in the future. Unlike interfaces however, each class can only extend one class, so you should extend classes with caution from this point of view. Abstract classes also allow you to inject behaviour to one class and have it automatically spread through its child classes. This usually makes certain sections of development/maintenance easier.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • On the other hand, you can make changes to an abstract class without breaking existing code. C# best practices from MSFT suggest to use abstract classes when designing systems when possible so as to minimize the need for breaking changes in the future. – Michael Graczyk Aug 07 '12 at 05:32
  • @MichaelGraczyk: I agree, hence my assumption in the first line: *assuming that your Abstract Class will only contain abstract methods*. I will add it to the last section of my response though – npinti Aug 07 '12 at 05:37
  • @Michael/npinti: I understand that we can make changes to an abstract class without breaking changes but not possible in Interface. But if u are adding againg an abstract method to an existing abstract class, still it breaks as interface... Does it make any diff or flexibility here? – Mohan Kumar Aug 07 '12 at 05:41
  • 1
    @mhn The added flexibility comes from being able to extend your framework without breaking users of the framework. You can add new virtual members to an abstract class with a default implementation, but cannot add new members to an interface. For example, MSFT added the `System.IO.Stream.CopyTo` method in .NET 4.0. They could not have done this were `Stream` an interface. – Michael Graczyk Aug 07 '12 at 05:46
0

In this case there is no difference but CTest class has the only class which could be inherited as a Class . However ITest interface can be inherited by other class and interface at the same time.

Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
0

In the scenario you have mentioned, that there is only one method, which will have no definition, the best way to go for is interface.

The major advantage an interface gives in Java that you can implement more than one interfaces, but you can extend only one class. So if you are already extending the one abstract class, you are not left with an option of extending any other class.

Golden rule: Interface is better than abstract class if we only need to define methods and not declare them.

Having said that an interface is better in your case, a programmer should also think of his code from a future perspective. Do you feel the class/ interface you are creating will have more methods in future. Would you like to define those methods or just declare? Answer to these question will let you know if an interface is sufficient or will need an abstract class.

Kamal
  • 5,462
  • 8
  • 45
  • 58
0

Advantage:

Implementation of Abstract class is better than Interface because method looking up of abstract class is fast than interface. If you modify your interface , you have to update your implementation class but any modification of abstract class , no effect on implementation class.

disadvantage:

If you want to implement more than one parent class method , it is not possible.
But regarding to interface you can implement more than one.
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37