0

What's the utility of interfaces other than abstraction and providing a workaround for multiple inheritance ?

If there is an Interface I having method m1() which is implemented by class A and another class B wants to access the method m(), what is the need of interface here.

Can we simply not implement that method in class A? like -

public class A implements I {
  public void m1() {
  // business logic goes here
 }
}

public class B {
   A objectOfA = new A();
   objectOfA.m1;
}
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
user3137592
  • 103
  • 3
  • 14
  • 2
    That's not a _workaround_, that's a basic feature of OOP. Read some decent resource about, and you'll get a better grasp of it. – moonwave99 Jan 03 '14 at 12:50

5 Answers5

3

This is a basic Object Oriented Programming problem. I suggest you to read OOP. Interface help to decouple your design and implemention, make it easier to reuse code. Also recomand some materials about design patterns. Head First Design Patterns is a good start and not that hard.

Zhou Tai
  • 214
  • 1
  • 10
2

Put simply, an Interface is a contract. A good example is the List Interface.ArrayList and LinkedList implement the List Interface. We know that. You also know that java.util.Collections provides methods for interfaces, like sort().

enter image description here

The point is, this very code can be used to sort() either the ArrayList or a LinkedList, because they implement the List interface, but you can also write your own code to implement more cooler things.This way, people can use your code without having to ask you to support their classes.

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
1

Yes we can simply implement that method in class A. But let be give a example of Interface so that you can understand your code. There is a concept of Re-usability in OOPs.

An interface defines a new secondary datatype in Java.

An interface is a reference type only its objects cannot be created.

An interface can inherit another interface but cannot inherit any class.

A class cannot inherit any interface but it (a class) can implement zero to many interfaces. If a class implements interfaces then

1) It has to override all the abstract methods of all the implemented interfaces.

2) Type compatibilty gets created between the interface and the class. It allows an interface reference can refer to object of implementing class. */

interface Iface
{
  int x = 3;//final and public by default
  void f();//abstract and public by default
}

interface AnotherI extends Iface
{ 
  //more declarations possible here
}

class InterfaceDemo implements Iface
{
 public void f()
 {
   int i;
   for(i =0 ; i< x; i++)
     System.out.println("Interfaces are widely used");
 }

 public static void main(String args[])
 {
   Iface ref = new InterfaceDemo();
   ref.f();//allowed
   //ref.newMethodsOfClass();//not allowed
 }
}
AJ.
  • 4,526
  • 5
  • 29
  • 41
0

A practical example

List<String> list;
list = thereIsMuchData ? new ArrayList<>() : new LinkedList<>();

public void f(List<String> strings) { ... } 

List being an Interface, ArrayList and LinkedList implementing classes.

You can see the following:

  • The implementation is your choice, based on technical knowledge. In this java distinghuishes itself from simple languages where there is one-fit-all List.

  • Usage of list is not overspecific.

  • The method f can handle all kind of lists.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

I agree with @IdleMind. Also interface force you to implement all the method(s) containing the interface. You can say it is a contract to your concrete class(s) where you have implemented it.

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26