Hello sireesha.
An interface is like a list of methods. Those classes that implement that interface must define those methods. An interface on it own is not an object because it is not a subclass of the Object class. A nice feature about interfaces is that you can give functionality to classes that are not related but have similar behavior. Lets say we have the interface Movable:
public interface Moveble{
public abstract void move();
}
Note that method definitions of interfaces are empty. Now, lets say we have a class Car and a class Animal that implements the Moveble interface. They are not related but they can both move.
So now we have
public class Car implements Moveble {
private int position = 0;
public Car(){
}
public void move(){
position += 5;
}
}
and
public class Animal implements Moveble {
private double position = 0;
public Animal (){
}
public void move(){
position += 2.5;
}
}
Note that the classes are very different but have the move() method in common. Now to answer this:
Originally Posted by sireesha
Can we create Objects for interfaces ?
No, but you can create a variable for classes that implement that interface and use the methods of that interface. Any object that is an instance of a class that implements that interface can be assigned to that variable:
Moveble myMovebleObject = new Animal();
myMovebleObject.move();
or
Moveble myMovebleObject = new Car();
myMovebleObject.move();
I hope this cleared things up.
This code has been checked by hand. Please correct me if I made a mistake.