Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The importance of interfaces in Java [closed]

Tags:

java

interface

Let's say we have two classes, Tiger and Aeroplane. One thing in common for these two types is the Speed. I know that it would be illogical to create a superclass ClassWithSpeed and then derive subclasses Aeroplane and Tiger from it. Instead, it is better to create an interface that contains the method speed() and then implement it in Aeroplane and Tiger. I get that. But, we can do the same thing without interfaces. We could define method speed() in Aeroplane and method speed() in Tiger. The only (maybe very big) flaw it would be that we couldn't "reach" the objects of Tiger and Aeroplane through an interface reference.

I am beginner in Java and OOP, and I would be very grateful if someone explained to me the role of interfaces. Cheers!

like image 354
Want Avatar asked May 26 '13 16:05

Want


People also ask

What is the importance of interface in Java?

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 are the importance of interfaces?

These interactions between your system and others are interfaces. Identifying interfaces helps you to define your system's boundaries. Identifying interfaces also helps you understand the dependencies your system has with other systems and dependencies other systems have with your system.

Why did we suddenly need interfaces in Java?

We need interfaces : To achieve total abstraction. To achieve security. Java doesn't allow multiple inheritance but it can be achieved by implementing multiples interfaces.


2 Answers

It's:

public int howFast(Airplane airplane) {     return airplane.speed(); }  public int howFast(Tiger tiger) {     return tiger.speed(); }  public int howFast(Rocket rocket) {     return rocket.speed(); }  public int howFast(ThingThatHasntBeenInventedYet thingx) {     // wait... what? HOW IS THIS POSSIBLE?!     return thingx.speed(); }  ... 

vs

public int howFast(Mover mover) {     return mover.speed(); } 

Now, imagine having to go back and change all those howFast functions in the future.

Interface or no, each class will have to implement or inherit the speed() method. An interface lets you use those disparate classes more easily, because they've each promised to behave in a certain way. You'll call speed(), and they'll return an int, so you don't have to write separate methods for every possible class.

When you find you need to handle speed differently because of a breakthrough in relativistic physics, you only need to update the methods that call speed(). When your great-granddaughter writes a class for HyperIntelligentMonkeyFish, she doesn't have to disassemble your ancient binary and make changes so that your code can monitor and control her mutant army. She needs only declare that HyperIntelligentMonkeyFish implements Mover.

like image 184
maybeWeCouldStealAVan Avatar answered Sep 19 '22 20:09

maybeWeCouldStealAVan


Interfaces allow Java, since it is statically typed, to work around multiple inheritance limitations to the degree felt worthwhile by the language designers.

In a dynamic language (like Python, Ruby, etc.) you can just call the speed method on any arbitrary object and discover at runtime if it is there or not.

Java, on the other hand, is statically typed, which means the compiler has to agree that the method will be there ahead of time. The only way to do that on classes that don't share common ancestry, and may only have Object in common, is an interface.

Since objects can implement an arbitrary number of interfaces, this means you get the goodness of multiple inheritance (a single object that can pose as multiple different objects for different methods) without the downside (of conflicting implementations in the two super classes).

With Java 8, the designers of the language concluded that interfaces without any implementation was overly restrictive (they didn't like my solution I guess ;-)), so they allow for a default implementation, thus expanding the multi-inheritance options of interfaces, while still trying to avoid the conflicting implementation problem via a complex set of precedence rules so that there is an unambiguous default implementation to execute.

like image 23
Yishai Avatar answered Sep 16 '22 20:09

Yishai