1

Code with an Interface

/* FileName : cat SampleInterface.java */
public interface SampleInterface
{
        public void draw();
        public void color();
}

Code with and abstract method

/* FileName : SampleAbstractMethod.java  */
abstract class SampleAbstractMethod
{
        public abstract void draw();
        public abstract void color();
}

In what ways are they both different ?

Well surely there has to be a big difference, Java developers would not have brought in interfaces if abstract methods was sufficient. Things that I see in common are

  1. Both cannot have definitions, like the code I have posted.

  2. Inheriting or the implementing class will have to bring in the definition.

Now as far I see it abstract methods can do what interfaces can, then why have interfaces ?, What is so special about an interface that a abstract method cant do ? For what exact reason need and interface ?

EDIT : My question is about the difference between abstract methods and interfaces, NOT abstract classes which the duplicate links point me to. So please explain how my question is similar to the marked duplicates.

vikkyhacks
  • 3,190
  • 9
  • 32
  • 47
  • Both of the first two questions listed above are closed, but I think this duplicatse this question: [Interface vs Abstract Class (general OO)](http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo) – T.J. Crowder Sep 29 '13 at 12:56
  • 1
    a major drawback of interface as compared to abstract class is that if you want to update the interface,it would break your existing code that previously referred to that interface.. – Anirudha Sep 29 '13 at 13:01
  • abstract method may have implementation, but interface methods only have method signature – Balaji Dhanasekar Sep 29 '13 at 13:03

5 Answers5

2

Abstract classes are used only when there is an "is a" relationship; interfaces can be realized by classes, which don't have anything in common.

Abstract class can realize methods; interface can't realize methods.

Interface can only describe constant values and methods, but not realize them. All methods of an interface are public abstract by default, and variables are public static final.

In Java class can inherit (realize) many interfaces, but only one abstract class.

With an abstract class you lose the individuality of a class, which inherits it. With interfaces you just enlarge the functionality of each class.

Example of an interface is "flying": butterfly, bird or a plane. Example of an abstract class is "game": football, cricket or chess.

Update: As to methods, you can have abstract methods both in interfaces and abstract classes. You just have to choose where to use them. And this question leads you to differences between interfaces and abstract classes.

Scadge
  • 9,380
  • 3
  • 30
  • 39
  • My question is about `abstract methods` not `abstract classes` – vikkyhacks Sep 29 '13 at 13:07
  • @vikkyhacks The concept of abstract methods is intimately linked to that of abstract classes, you cannot consider one without the other – Richard Tingle Sep 29 '13 at 13:14
  • Anyway. In my answer I admitted that in interface methods are `abstract` by default. So you can have abstract methods in interfaces and abstract classes. So now you are to choose **where** to use them. And this question leads you to differences between interfaces and abstract classes. – Scadge Sep 29 '13 at 13:18
  • thanks, +1 for the answer, can you modify it a bit to my edit so that I can accept it, – vikkyhacks Sep 29 '13 at 13:39
1

Abstract classes may have non-abstract methods, whereas interfaces can only have method declarations. In some cases you want an abstract class that has some functionality, but its children must implement its abstract methods. A class may implement more than one interface, but may inherit from 1 abstract class.

Amir Kost
  • 2,148
  • 1
  • 16
  • 30
1

Its all about the possibility ;-)

In an interface you have only the possibility to make a function declaration. So its somehow a limitation you cannot even define variables (Constants you can) ...

In abstract classes you can do both! You can have functions with and without source code. You can have variables and so on ...

The big thing is that you can only use one abstract class as parent but as many parent interfaces as you want.

So its really a question of what you need in your case and how you take advantage of the OOP concept.

If you just want to reduce code its OK to use it without thinking but if you want to make something more reliable read more about Subtyping

pknoe3lh
  • 374
  • 3
  • 10
1

Your question is about the difference between abstract methods and interfaces. But I think you are forgetting one thing that all the methods in an interface are abstract. So the methods in an interface and an abstract are same.

Well, the need for an interface is to fulfill the need of multiple inheritance. Abstract classes are used when you need to create a concrete class, but want to make sure that there is some common state in all the subclasses or a possible common implementation for some operations.

Madhura
  • 551
  • 5
  • 18
0

Take a look at official documentation at Oracle: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html (Abstract classes) and http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html (interfaces).

And here is nice discussion at Stack Overflow about abstract classes vs interfaces: Abstract class vs Interface in Java

Community
  • 1
  • 1
Ernestas Kardzys
  • 1,719
  • 2
  • 16
  • 21