Interface in Java





  • Interface can have only abstract methods. Since Java 8, it can have default and static methods.

  • All methods are by-default public.

  • Interface has only public, static and final variables.

  • Interface can't have constructor.

  • Following modifiers can't be applied to the methods declared in an interface :-

    private, protected, final, synchronized.

  • Use interface keyword to declare interface.

Exampl :

interface Animal
{
   public void talk();
}
class Dog implements Animal
{
 public void talk(){
   System.out.println("bark");
 }
}
class Cat implements Animal
{
 public void talk(){
  System.out.println("mew");
 }
}
class MainClass
{
 public static void main(String[] ar){
    Animal ob = new Dog();
    ob.talk();
    Animal ob1 = new Cat();
    ob1.talk();
 }
}           
OUTPUT :
bark
mew         
  • An interface can extends one or more interface.

    interface A{
    }
    interface B extends A{
    }
    interface C extends A, B
    {}                  
  • One or more interfaces can be implemented in a class.

    interface A{
    }
    interface B{
    }
    class Test implements A, B{
    }                  
  • A class can extends one class and implements one ore more interfaces at the same time, but class will extends first and then implements interface.

    interface A{
    }
    class B{
    }
    class Test extends B implements A{
    }                  

Next topic is object-cloning




 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved