Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The purpose of interfaces continued

OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract. And I semi see the point of them.

But if all you have in the interface is:

 public interface animal{
  void eat(object food);
}

and it has no implementation as such, then whoever uses your interface has to write it from scratch, every time.

If you are creating a number of classes all implementing such features and the implementation is only slightly different, this is going to be a lot of hard work.

Any help to get my head around this is appreciated because I know it's really important.

like image 687
Julio Avatar asked Oct 29 '10 14:10

Julio


People also ask

What is the purpose of interfaces?

The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.

What is the purpose of the interface Mcq?

A interface can also extends another interface or multiple interfaces in java programming. It is used to achieve abstraction and multiple inheritance in Java. It can be instantiated, means, we can create an object of an interface.

Why do we extend interface?

Extending InterfacesAn interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

What's the purpose of interface in Java?

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.


1 Answers

Interfaces are the only way to create multiple inheritance in Java.

Say you create a class Animal. And all animals, including humans extend that. And each of those animals inherits common methods like eat, breathe, etc.

But now let's say you have a MathProblem class. And you want to have certain classes that can solve that problem by passing the problem to a solve(MathProblem problem) method. And you know that a Human, but also a Computer might solve the math problem. So they both need to be able to solve that problem. You might be able to get the Computer to extend some MathSolver class that has the method, but Human already extends Animal, and can't extends anything else. So a better way is to make MathSolver an interface and have both Human, Computer, and any other classes that need to solve problems implement that.

Also note that a Human and a Computer might solve the problems in completely different ways, since their such different objects. That's what interfaces are best for. Defining certain abilities that cut across multiple inheritance hierarchies, and can have very different implementations, but can all be passed to a method that accepts any of them. Think of the Comparable interface; it's not something a certain class of objects has, all sort of things can be compared, and usually in very different ways. But you can always call sort on a List of Comparable objects since you know they have a certain order, no matter if they're Numbers, Animals, Computers or anything else (as long as they implement Comparable and define their ordering).

like image 178
Andrei Fierbinteanu Avatar answered Sep 21 '22 07:09

Andrei Fierbinteanu