Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Interfaces in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 302
    Comment on it

    Interface:

    • Interface is similar to class and represented by interface keyword. It contain only pure abstract methods means only declaration of methods is given inside interface and the class which implements it write the body for that method.
    • With the help of interface we can achieve multiple inheritance. As multiple inheritance is not possible through extending class , we can extend just one class at a time this is due to the Ambiguity arises. But a class can implement multiple interfaces.

    Example:

    interface draw{  
    void print();  
    }  
    
    class Sub implements draw{  
    public void print()
               {
                   System.out.println("Hello FindNerd");
               }  
    
    public static void main(String args[]){  
    Sub obj = new  Sub();  
    obj.print();  
     }  
    }  
    

    Output:

    Hello FindNerd

    Multiple inheritance by interface

    interface Print
      {  
    void print();  
    }  
    
    interface Display
     {  
    void show();  
     }  
    
    class AClass implements Print,Display{  
    
    public void print(){
            System.out.println("Hello");
                            }  
    public void show(){
               System.out.println("Welcome to FindNerd");
                         }  
    
    public static void main(String args[]){  
     AClass obj = new AClass();  
    obj.print();  
    obj.show();  
     }  
    }  
    

    Output:

    Hello

    Welcome to FindNerd

    In the above example there are two interfaces Print and Display they are having abstract methods print() and show() and Aclass is extending both interfaces and implementing both abstract methods.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: