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.

I was thinking about that, and I had some doubts.

When I declare an interface, for example:

public interface MyInterface
{
   public void method1();
   public void method2();
}

Could these interface methods be considered abstract? What I mean is that the concept of an abstract method is:

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon).

So, could these methods be considered abstract? They are not "pure" abstract methods as I'm not using the abstract word, but conceptually, it looks like they are.

What can you tell me about it?

Thanks.

share|improve this question
    
Probably best on SO –  billy.bob Sep 30 '11 at 11:33
4  
@billy.bob - I think it's too abstract for Stack Overflow. There's not a specific coding issue here. –  ChrisF Sep 30 '11 at 12:09
    
Is this Java code? –  Andres F. Jun 18 '13 at 21:56
    
no it's not. it's just an example. the question is not focused on any programming languages. –  rogcg Jun 19 '13 at 12:34
add comment

4 Answers

up vote 8 down vote accepted

An interface is like a "purely" abstract class. The class and all of its methods are abstract. An abstract class can have implemented methods but the class itself cannot be instantiated (useful for inheritance and following DRY).

For an interface, since there isn't any implementation at all they are useful for their purpose: a contract. If you implement the Interface then you must  implement the methods in the interface.

So the difference is an abstract class can have implemented methods whereas a interface cannot.

The reason they are separate is so a class can implement several interfaces. Java and C# restrict a class to inherent from a single parent class. Some languages allow you to inherit from multiple classes and you could accomplish the work of an interface via a "purely" abstract class. But multiple inheritance has its problems, namely the dreaded Diamond Problem

share|improve this answer
    
+1 for incorporating the difference between inheritance of an abstract class and implementation of one or more interfaces. –  user37772 Sep 30 '11 at 15:55
    
Diamond Problem is an awesome paradox. –  rogcg Sep 30 '11 at 16:43
    
I dissagree with 'An interface is like a "purely' abstract class" part. They are 2 different types of OO 'building blocks', so they are really not similar at all. They share some common features, but in nature they are different types, more like men and women for example :) –  Emmad Kareem Oct 2 '11 at 22:27
3  
@Emmand Kareem I do not dissagree with 'An interface is like a "purely" abstract class' part. That's why i wrote it :-). If you have any good reasons for your disagreement please post, id like to hear –  coder Oct 3 '11 at 12:59
add comment

I found an usefull answer here: http://download.oracle.com/javase/tutorial/java/IandI/abstract.html

All of the methods in an interface are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).

share|improve this answer
1  
Also note that an abstract class extends one other object. Interfaces have no notion of a superclass. –  user1249 Sep 30 '11 at 11:39
1  
Also note that you can implement multiple interfaces, but you can only inherit from one class, abstract or not. –  NullUserException Sep 30 '11 at 14:12
    
@ThorbjørnRavnAndersen: An interface can extend one or more interfaces. It isn't the same things as a super class, but it is a level of inheritance. –  unholysampler Sep 30 '11 at 14:41
    
You can implement multiple interfaces. Looks like the concept of multiple inheritance, but not exactly. –  rogcg Sep 30 '11 at 16:38
    
@unholysampler, which is not a superclass - as I said. –  user1249 Oct 1 '11 at 0:06
add comment

Abstract classes can have abstract methods.

Interfaces can only have abstract methods.

method1() and method2() in your example are abstract methods.

share|improve this answer
add comment

The difference here is that abstract classes can contain implementation details, although cannot be instantiated by themselves. Whereas an interface is merely a template for a class

share|improve this answer
7  
Abstract methods cannot contain implementation details. Abstract classes can. –  Matt H Sep 30 '11 at 11:46
    
I know the difference between a method and a class - but I can't understand the point you're making? –  billy.bob Sep 30 '11 at 12:04
    
Your answer states that abstract methods can contain implementation details - they can't. Just a typo? –  Matt H Sep 30 '11 at 12:07
1  
I fixed the typo. –  Martijn Verburg Sep 30 '11 at 13:35
    
@billy.bob this question is about the abstact method though. –  Chad Oct 3 '11 at 15:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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