-3

Possible Duplicate:
Interface or abstract class?
Abstract and Interface in java

I am still a student in Java and i came across abstract class and interface.

now when you create an abstract class you write methods like this:

public abstract void something();

but when you create a method in your interface it looks like this:

public void something();

Now im not blind i can see that there is some difference also i do know that you extend an abstract class and you implement an interface.

But can someone tell me what the difference is maybe an example of where you would use one over the other?

Community
  • 1
  • 1
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • i am sorry please delete my post then :( – Marc Rasmussen Nov 12 '12 at 23:57
  • 2
    Avoid asking questions about basics of Java. Google is always there to help you. Ask only if you did your research and still don't understand **but** do state in your question that you found so and so explanations, etc., but you're still confused. – ADTC Nov 12 '12 at 23:58
  • @ADTC Thank you i will remember that in the furture. – Marc Rasmussen Nov 12 '12 at 23:59
  • You're welcome. It's sad, many SO veterans don't bother guiding new users like you. They're so quick to mark duplicates and close questions. I don't want you to go through what I went through when I was first here. :) – ADTC Nov 13 '12 at 00:03
  • Actually, as far as questions go, it's not *that* bad. Sure, the web is full of information about differences between interfaces and abstract classes, but the interesting thing about this question is that it shows how Java's syntax can confuse beginners. Since every method in an interface is abstract, by definition, the `abstract` modifier isn't used. – Vojislav Stojkovic Nov 13 '12 at 00:04
  • 1
    Well I wouldn't say they're mean, but more like they're cold and indifferent. – ADTC Nov 13 '12 at 00:29
  • I know this is an old post, but +1 from me since I'm also very new and this thread helped make things clearer. – Josh Jul 31 '14 at 21:35

2 Answers2

2

There's no difference between those two routines. The difference lies in that abstract classes can contain common logic used for all implementations while interfaces cannot.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
2

"Abstract" basically means that the method has no implementation. The implementation has to be provided by subclasses. As a consequence, one cannot create instances of classes having abstract methods.

Now interfaces in Java are just a collection of method signatures. They cannot contain implementations by design. Thus, the abstract keyword would be redundant. One cannot create instances of interfaces, only of concrete classes that implement the interface.

Sjlver
  • 1,227
  • 1
  • 12
  • 28
  • Are interfaces _something like_ header files in C? :) Btw, I can create "instances" of abstract classes, but they should refer to instances of extending concrete classes. Like `AnAbstractClass x = new AConcreteClassExtendingAnAbstractClass();` (Sorry I don't know how to explain this properly hence the scare quotes.) – ADTC Nov 13 '12 at 00:39
  • @ADTC Interfaces are like a contract specifying what operations are supported by an object. You can use interfaces for example as function arguments, as in [Collections.sort(List list)](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List)). On the other hand, header files in C can contain arbitrary function definitions, not necessarily only definitions of abstract class methods. Header files can even contain implementations (of inline functions) – Sjlver Nov 13 '12 at 08:15