Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any true point to the Java interface? [duplicate]

Tags:

java

Possible Duplicate:
How are Java interfaces actually used?

I'm not talking from an accademic buzzword point of view but from a pratical developer point of view.

So taking the example:-

    Class1 implements Interface 
 public String methodOne() {
  return "This is Class1.methodOne()";
 }

 public String methodTwo() {
  return "This is Class1.methodTwo()";
 }
    }
Class2:

    Class2 implements Interface 
 public String methodOne() {
  return "This is Class2.methodOne()";
 }

 public String methodTwo() {
  return "This is Class2.methodTwo()";
 }
    }

Using the Interface:-

 Client {
  Interface intface = new Class1();

  intface.methodOne();
  intface.methodTwo();

  Interface intface = new Class2();
  intface.methodOne();
  intface.methodTwo();
}

But what are the benefits over just writting:-

 Client {
Class1 clas1 = new Class1();

clas1.methodOne();
clas1.methodTwo();

Class2 clas2 = new Class2();
clas2.methodOne();
clas2.methodTwo();
 }

And bypass the Interface altogether.

Interfaces just seem to be an additional layer of code for the sake of an additional layer of code, or is there more to them than just "Here are the methods that the class you are accessing has"?

like image 434
mark Avatar asked Jul 13 '10 08:07

mark


People also ask

Which statement about the collection interface is true?

The only true statement is C), All methods defined in an interface must be implemented when used by another class.

What will happens if two interface have same method name?

A class implementation of a method takes precedence over a default method. So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.

What is the point of a Java interface?

In Java, an interface specifies the behavior of a class by providing an abstract type. As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported through this technology. Interfaces are used in Java to achieve abstraction.

What is false about interface in Java?

Q) False statement about Java interfaceIt can be instantiated, means, we can create an object of an interface. There can be only abstract methods in the interface not method body.


1 Answers

When using standalone classes, you don't need interfaces. However, when you have a type hierarchy, interfaces are indeed indispensable.

Your simple example does not really do justice, but let's rename your interface to something more useful and concrete, e.g. a Sorter algorithm which can take a list of items and sort them. You can have several different sorting algorithms implemented, and you may want to change the algorithm used, based on the context (i.e. QuickSort is faster for large data sets, but BubbleSort is better for small data sets, etc.) So you don't want to tie the client code to one specific algorithm. By using the polymorphic Sorter type (implemented as an interface), you can pass different concrete sorter objects to the client, without it knowing (and caring) what algorithm it is actually using. And you can any time introduce a better sorting algorithm, or remove one which proved inefficient, without the clients noticing anything.

Such a feat would be impossible without interfaces. The alternative would be calling the sort method of choice directly from (possibly duplicated) if-else or switch blocks all over the place, with the inevitable possibility of introducing bugs when forgetting to update all places properly when a sorting algorithm is added/removed... not to mention that you would need to recompile all client code after each such change :-(

like image 109
Péter Török Avatar answered Oct 12 '22 12:10

Péter Török