0

Possible Duplicate:
When to use abstract classes instead of interfaces and extension methods in C#?

When I read and looked at codes using Abstract classes, I was able to justify it because it allows you to add common methods for any subclasses extending the abstract class. So for example, if objects behavior is similar, I would use Abstract classes to implement bodyless abstract methods that is required for each object, and simply use non abstract methods already implemented in the abstract class. I can think of a scenario dealing with multiple media file types (avi,mpg,mp4) and you would have common methods for all files, as well as media specific abstract methods that needs to be implemented.

However, I am a bit confused as to why you would knowingly create an interface which cannot contain any non-abstract methods. Reading this page, it states that it hides information (you mean the abstract methods?).

Hiding details and providing common interfaces is called encapsulation, which is an analogy from making an object look like it's covered by a capsule (the interface in this case). This allows two objects differing in internal representation but having the common interface interchangeably usable (called interchangeability). Interfaces also allow to facilitate the use of data structure and guard the state of the object from invalid inputs and modification of the structure.

So does this mean that any objects which share the common behaviors implemented uniquely can be treated like they are the same category? So for the media file example, does this mean any specific media file type implementing the interface MediaFile are to be passed as arguments for a method dealing with such type of objects?

public class ServiceClass {
    public ServiceClass(){
    //no-args constructor   
    }

    public boolean runService(ICollaborator collaborator){
    if("success".equals(collaborator.executeJob())){
        return true;
    }
    else
    {
        return false;
    }
}
}

But can't you do above with an abstract class? Also, isn't the ability of having a non-abstract method better than having none at all for the future when you suddenly need to have an existing method that will apply to all classes extending the abstract class?

Or is the difference of using Interface, the ability to protect the implementation of data completely? Once again, I don't see what cases you would use it. Other than that I implement ActionListener quiet often to have the actionPerformed method.

3

2 Answers 2

4

To cut a long story short, (since this question for some reason is not getting closed,) interfaces are essential for single-inheritance languages like Java and C# because that's the only way in which you can aggregate different behaviors into a single class.

In other words, when you write class A which expects to be passed a reference to some B thing to work with, it helps if you declare B to be an interface and not an abstract class, because if it is a class then you will be forcing the author of class C who wants to write C so that it works with your A to derive their class from class B, while they may have other plans for it, which may demand that C derives from some other class D, which you may have never heard of. By writing A so that it accepts an interface of type B instead of an abstract class B, the author of C is free to derive his class from D and also implement interface B for the sake of working with your class A.

5
  • 6
    If you posted a link to some of these duplicates, the question would be closed that much faster...
    – Oded
    Commented Jan 8, 2012 at 12:58
  • 1
    @Oded Nah, the OP appears to have some specific concerns, and in order to tell for sure whether his particular concerns are addressed by another question I would have to do a lot of reading, which I do not want to do.
    – Mike Nakis
    Commented Jan 8, 2012 at 13:01
  • 1
    @MikeNakis You should post the dupes in a comment if you have them handy, even if you don't vote to close as a dupe. That way the OP can checkout the similar questions and get quite more info on his/her issue.
    – yannis
    Commented Jan 8, 2012 at 14:36
  • 3
    your second paragraph was kind of confusing...but your first paragraph sums it up nicely. Commented Jan 10, 2012 at 16:15
  • javastudent, I was afraid someone would say that. And for me it is a personal failure when the OP says it. I am embarrassed. Okay, It helps if you draw a diagram with A at the bottom, B next to it, D further next to it, and C a little bit above them all, and then consider the interactions between them as you read the paragraph. In any case, if you want me to clarify any part of that paragraph in writing, I can do it, just tell me the part(s). Also, perhaps I should not have begun the paragraph with "in other words". It is a further explanation, not an alternative explanation.
    – Mike Nakis
    Commented Jan 10, 2012 at 19:14
1

Interfaces are used in situations where the object doesn't matter only that it has a particular interface defined. For example:

function (Interface Object)
{
    // Use a method defined in Interface.
    Object.Method();
}

Then we can pass any object to this function knowing that all objects (whatever they might be) have the interface defined so we are safe to use those methods.

More information here: https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface

2
  • 5
    An abstract class would do just fine in your example. Doesn't really answer the question.
    – Oded
    Commented Jan 8, 2012 at 12:49
  • Why use an abstract class when a normal class would do, etc..? Yes an abstract class would 'do' in this example but that is not the right way to do it, especially if you are only interested if an object implements a particular interface. This is the fundamental difference in why you would choose one over the other. If you notice in the function i am constraining to an interface not an object, that is a huge difference. Commented Jan 8, 2012 at 13:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.