Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of usefulness of interface in java

Interfaces consists of abstract methods and final variables. Well, it is used as a generalized contract put forth so that classes implementing it should follow the rules by implementing methods in it.

Is this the only use/scope of interface in Java? Have they introduced the concept of interface only for this, or am I missing something? Please help me in understanding the use of interfaces, with examples. (Not on how to use or create interfaces, but to show how they are helping programmers).

Thank you.

like image 373
Anuj Balan Avatar asked Jan 25 '12 07:01

Anuj Balan


People also ask

What is the scope of interface in Java?

An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction.

What is the scope of method in interface?

The whole point of an interface is that it exposes methods to the outside world so implementation details can be hidden. What happens inside the interface should not be known to the outside world.

Why interface is useful?

Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship. Declaring methods that one or more classes are expected to implement. Revealing an object's programming interface without revealing its class.

What is the default scope of interface?

All abstract, default, and static methods in an interface are implicitly public , so you can omit the public modifier. In addition, an interface can contain constant declarations. All constant values defined in an interface are implicitly public , static , and final .


3 Answers

Here's where I understood their usage when I first read about them:

Say that you receive a portable disc player as a gift. When you try to operate the player, nothing happens --- the player requires batteries. What batteries fit into the player? Fortunately, on the back of the player is the specification, This player requires two AA batteries.'' With this information, you can obtain the correctly sized components (the batteries) and fit them into the player. The completed assembly'' operates.

The specification of the disc player's batteries served several useful purposes:

The specification told the user which component must be fitted to the player to ensure correct operation. The specification told the manufacturer of the disc player what size to build the player's battery chamber and what voltage and amperage to use within the player's electronics. The specification told the battery manufacturer what size, voltage, and amperage to build batteries so that others can use them. These three facts are important in themselves, but they also imply that the user, the disc manufacturer, and the battery manufacturer need not communicate directly with each other --- the specification of the battery is all that is needed for each party to perform its own task independently of the other two.

like image 169
isah Avatar answered Sep 20 '22 01:09

isah


I will give you an example.

You have a car class

    class Car{
        start(){
            //code
        }
        stop(){
            //code
        }
    }

And you want a Super Car which needs to bounce(but Bounceable feature belongs to ball/rubber)

Here your Super Car can implement bounceable

    public interface Bounceable{

        bounce();
    }

Now you got Super Car car which can bounce.

    class SuperCar extends Car implements Bounceable{
        //My Super Car will bounce.
    }
like image 40
Jayy Avatar answered Sep 21 '22 01:09

Jayy


As you have said, interfaces are used to specify contracts which classes that implement them must follow. The scenarios in which they are used is usually a scenario where you call a class which implements a particular interface. The fact that the implements a particular interface provides you with the knowledge that that given class, does indeed implement a given set of methods. Usually you do not care what happens in these methods, what matters to you is that the class has these methods.

As an example, you might want to consider the Observer Design Pattern. Basically, you have one object that has a state which it shares with other objects and passes to them notifications when some of its data changes.

So, if for instance all the observers implement the following interface:

public interface Observer
{
 notify();
}

The Subject can, without any knowledge what so ever about it's observers, do something like this:

public class MySubject...
{
  List<Observer> observers;

  .....



  public void notifyAll()
  {
      for (Observer observer : observers)
      {
         observer.notify();
      }
  }
}

As you can see, the MySubject class does not have any knowledge of how the Observer implements the notify method, what it really cares about is that it implements it. This allows you to create different observers which have different ways of implementing their own notify class without requiring any changes in the MySubject class.

Interfaces also allow for certain type safety. Interfaces are usually also handy when you have a series of classes implementing a series of similar behaviours which you usually want to store into some type-safe data structure. This is usually a good work around the fact that Java does not allow multiple inheritance. So, in the example provided above, I can have an indefinite amount of different classes. But as long as they all implement the Observer interface, they can all be added to the same list.

In the notifyAll methods, then I can just iterate of the elements of the list instead of checking if the element is of a certain type and then casting it, which can introduce some extra overhead in the program's execution.

like image 42
npinti Avatar answered Sep 21 '22 01:09

npinti