Anonymous

Why Do We Use Interface In Java ?

11

11 Answers

Anonymous Profile
Anonymous answered
Go through this program once ...
I will explain the use of interface at the end...

Interface Interf
{
void m1();
void m2();
}
class InterfaceTest implements Interf
{
public void m1()
{
System.out.println("implementation m1");
}
public void m2()
{
System.out.println("implementation m2");
}
public void m3()
{
System.out.print("YOU WONDER ! I knew why????? I am not instatiated by interface instance I okkkk  I am an instance of InterfaceTest .....  Well done you understand interfac concept now");
}
public static void main(String arg[])
{
Interf I=new InterfaceTest();
I.m1();
I.m2();
InterfaceTest b = (InterfaceTest)I;
b.m3();
}
}

Now forget the INTERFACE part in my program and follow me...it is simple..... Come on scroll...
If you want to reuse the method m1 and m2 in some other classes   means , what would you do????
Way 1 : I can instantiate an object for class InterfaceTest at my class .
DISADVANTAGE : If you instantiate another object means its implicitly tells that you create an copy of InterfaceTest class. So it wont come under the category REUSE..

Way 2 : I can extend InterfaceTest in my class ...well done good but
DISADVANTAGE : If interfaceTest contains lot of methods other than m1 and m2 means you have to allocate memory for that methods also(waste na) in your class

Way 3 :    USING INTERFACE >>>>>>
    make m1 and m2 methods inside an interface (now see above program once again completely)

so you can accesses it from another class like extend but one small difference you are not pointing the unwanted method m3.....good luck...

Ask questions if you need clarifications...
Anonymous Profile
Anonymous answered
When we implement an interface we are bound to implement all the methods in the implementing concrete class. So we are sure that all the functionality that the caller requires will be coded in the class and we won't miss any functionality. It I beneficial especially when we have a class A which calls function of class B. After some time suppose we want to replace class B with C we can switch to class C as without any concern as long as we know that Class C also implements that same interface as the class B

Sandeep Hooda.
Gopinath Sundaram Profile
Interfaces is one of the best used concepts in Java.let me explain this with an example : Say you designed a gps device for car which looks into the map and automatically turns the car to the direction as seen in the map.This GPS device can be used in many cars like benz,fiat,etc.For each car,the mechanism of turning the left or right may differ depending on the implementation of the car system.So,these functions should be written by the car manufacturer and henve these methods are put in a interface,which is implemented by the car manufacture as per his car implementation.Interface include only a set of function declarations which are to be defined by the car manufacturer(In this case).Got it?
Anonymous Profile
Anonymous answered
In the Java programming language, an interface is similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies.There are no objects.Interfaces cannot be instantiated. They can only be implemented by classes or extended by other interfaces.
Anonymous Profile
Anonymous answered
Because interface is provide only structure  of method without show any type of implementation of method.
Anonymous Profile
Anonymous answered
Interfaces are used to implement the concept of late-binding.
Here late binding is the process of passing functionality to a function dynamically.
Actually interfaces are not used to implement the concept of multiple inheritance.
Inheritance represents inheriting properties from the super class in  classes where as in interfaces when you extend   an interface the interface can get only declarations then the implementing class has to provide definitions so we cannot implement multiple inheritance
just people feel that interfaces supports multiple inheritance but it is not right.
Anonymous Profile
Anonymous answered
If the requirement is like that something in your design changes frequently then go for interfaces instead of classes.

For example if you want to your project should support different databases . So that client can change his database in future we use interfaces contains property procedures in class file with out altering objects.
Gopinath Sundaram Profile
Interface provides for flexibility and tends to resolve issues on vendor specific details by giving them the functions to implement.
Anonymous Profile
Anonymous answered
It is mostly used when you are using polymorphism...when you need to casting some objects with IS A relation ships...

Answer Question

Anonymous