20

Possible Duplicates:
Abstract class and Interface class?
Java: interface / abstract classes / abstract method

In Java, whatever use of interface is fulfilled by abstract class. I know one advantage of interfaces is that if we implement an interface then we can also extend another class. Is there any other use or advantage of interface in Java?

khelwood
  • 55,782
  • 14
  • 81
  • 108
kandarp
  • 4,979
  • 11
  • 34
  • 43
  • 1
    This question has been asked many times before, in different guises. – Stephen C Dec 14 '10 at 06:09
  • 1
    To implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. This way interface allows a class to become more formal about the behaviour it promises to provide. – Ahmed Raza Jul 16 '16 at 19:15

6 Answers6

62

Interfaces allow you to use classes in different hierarchies, polymorphically.

For example, say you have the following interface:

public interface Movable {
    void move();
}

Any number of classes, across class hierarchies could implement Movable in their own specific way, yet still be used by some caller in a uniform way.

So if you have the following two classes:

public class Car extends Vehicle implements Movable {
    public void move() {
       //implement move, vroom, vroom!
    }
}

public class Horse extends Animal implements Movable {
    public void move() {
       //implement move, neigh!
    }
}

From the perspective of the caller, it's just a Movable

Movable movable = ...;
movable.move();  //who am I?

I hope this helps.

Todd
  • 3,438
  • 1
  • 27
  • 36
  • 2
    thanks to help me to understand difference between interface and abstract with proper example. – kandarp Dec 14 '10 at 06:37
26

What you like : thousands of abstract methods in one Abstract Class and inherit this class OR make as many interfaces for specific abstract methods and use those only you want by inheriting as many interfaces as needed...

abstract class A
{
 //thousands of abstract method();
 abstract methodA();
 abstract methodB();
 abstract methodC();
}

//OR
interface ForOnlymethodA
{
 void methodA();
}
interface FormethodBandmethodC
{
 void methodB();
 void methodC();
}

So, use that method only what you just need by inheriting particular interface, if you are inheriting Abstract classes then you are unnecessarily inheriting all methods that you don't need in one class and may be needed in some other classes..

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
FosterZ
  • 3,863
  • 6
  • 38
  • 62
7

Multiple interfaces can be implemented, but only one class can be extended. A completely abstract class is a lot like an interface, except that an abstract class can contain variables.

It really depends on what you need. C++ allows you to extend as many classes you want, and it turns into a bit of a disaster. The nice thing about having only one superclass is that there's only ever one other set of implementations that you have to worry about (even if the parent has a parent, the parent's particular combination becomes your parent...)

Interfaces allow one object to play many roles, but they don't allow code reuse.

It's really to simplify thinking about inheritance. On the balance, I think they got it right.

Robert
  • 6,412
  • 3
  • 24
  • 26
1

Interfaces allow the nominative typing in Java to work across disjoint class hierarchies.

This is due to the "single inheritance" limitation on a class hierarchy and "closed types". I will hold my tongue on subtype polymorphism and Java's implementation of it ;-)

There are other solutions to this problem such as dynamic typing, structural typing, multiple inheritance, and traits, etc. Each approach has advantages and dis-advantages. Interfaces were just the approach that Java took.

1

Advantages over an abstract class? except the fact you can implement multiple interfaces but extend only one (abstract or not) class, it's the same as an abstract class that all of it's methods are abstract and public

Eran Medan
  • 44,555
  • 61
  • 184
  • 276
-1

Java interface - provides the data encapsulation which means, implementation of the methods can not be seen. The class which extends this interface must implement all the methods declared in it.

more info: wiki answer

Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84