Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the actual use of interface in java? [duplicate]

Tags:

Possible Duplicates:
Abstract class and Interface class?
Java: interface / abstract classes / abstract method

In Java, whatever use of interface is fulfilled by abstract class. I know one advantage of interfaces is that if we implement an interface then we can also extend another class. Is there any other use or advantage of interface in Java?

like image 690
kandarp Avatar asked Dec 14 '10 05:12

kandarp


People also ask

What is the actual use of interface in Java?

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables.

What is interface real life example?

The Interface is a medium to interact between user and system devices. For example, in our real life, if we consider the interface to be given as an example is the air condition, bike, ATM machine, etc.

Why multiple interface is used in Java?

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.

What is the purpose of an interface?

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.


2 Answers

Interfaces allow you to use classes in different hierarchies, polymorphically.

For example, say you have the following interface:

public interface Movable {
    void move();
}

Any number of classes, across class hierarchies could implement Movable in their own specific way, yet still be used by some caller in a uniform way.

So if you have the following two classes:

public class Car extends Vehicle implements Movable {
    public void move() {
       //implement move, vroom, vroom!
    }
}

public class Horse extends Animal implements Movable {
    public void move() {
       //implement move, neigh!
    }
}

From the perspective of the caller, it's just a Movable

Movable movable = ...;
movable.move();  //who am I?

I hope this helps.

like image 187
Todd Avatar answered Sep 21 '22 01:09

Todd


What you like : thousands of abstract methods in one Abstract Class and inherit this class OR make as many interfaces for specific abstract methods and use those only you want by inheriting as many interfaces as needed...

abstract class A
{
 //thousands of abstract method();
 abstract methodA();
 abstract methodB();
 abstract methodC();
}

//OR
interface ForOnlymethodA
{
 void methodA();
}
interface FormethodBandmethodC
{
 void methodB();
 void methodC();
}

So, use that method only what you just need by inheriting particular interface, if you are inheriting Abstract classes then you are unnecessarily inheriting all methods that you don't need in one class and may be needed in some other classes..

like image 29
FosterZ Avatar answered Sep 23 '22 01:09

FosterZ