Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

What is the difference between abstract classes and interfaces in java?

And under what circumstances should I choose to create an abstract class or an interface. What are the points that I must have in my mind before choosing one of the above?

share|improve this question

marked as duplicate by Doc Brown, Walter, ChrisF Dec 19 '12 at 14:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
This question has been asked a zillion times. –  user61852 Dec 19 '12 at 14:03
add comment

1 Answer

up vote 1 down vote accepted

An abstract class may contain implementations, interfaces don't.

Example of an interface:

public interface MyInterface {

    public void interfaceMethod();

}

Example of an abstract class:

public abstract class BaseClass {

    // this is not possible in an interface
    public void baseMethod() {
        stubMethod();
    }

    public abstract void stubMethod();

}

As to why; classes are allowed to do multiple inheritance with interface types but not with abstract classes. The reason behind this is to avoid the diamond inheritance problem, a problem that boils down to the question on which method to use if two implementations are inherited.

Both are used frequently and you can start by reading up on some examples from the GoF book Design Patterns.

share|improve this answer
add comment

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