java interface

Interface in java with examples:

                                The collection of abstract methods and final variables protective into a single unit is called as an interface. Java Interface is similar to a class in java. It is a blue print of class, the interface consisting only abstract methods and final variables. The interfaces are used for future implementation purpose.
By using interface we can achieve 100% abstraction. The java Interface also represents IS-A relationship. We can create a reference for an interface, but we can’t create an object for the interface.
In interface using only final variables that are constant, so we can’t change the value of the variable. The interface keyword is used to declare an interface.
By using interface we can achieve multiple inheritance.

Syntax:
                Import java.lang.*;
                public interface InterfaceName
                {
                //abstract methods       
                //final variables
                }

Example:
                public interface flyable
                {
                //abstract method
                Public void fly();               
                }

                public class Bird implements flyable
                {
public void fly()
                {
                System.out.println(“ Am bird”);
                }
public static void main(String args[]){
Bird b = new Bird();
b.fly();
}
}

Interview Questions:

What is an interface?
How can we achieve multiple inheritance?
Is it necessary to add public abstract keywords in an interface?
What are the differences between abstract class and interface?
How can we change variable value interface?

Differnces between abstract class and interface


EmoticonEmoticon