Java – Interfaces

Interface:

  • Interface allow to define only abstract methods.
  • Interface methods are ‘public abstract’ by default.
interface Sample
{
            void m1();
            void m2();
}

implements:

  • ‘implements’ is a keyword.
  • Interface must ‘implements’ by class.
  • Implemented class override all abstract methods of an interface.
interface First
 {
            void m1();
            void m2();
}
class Second implements First
{
            public void m1(){
                        System.out.println(“m1….”);
            }
            public void m2(){
                        System.out.println(“m2….”);
            }
}
class Main
{
            public static void main(String[] args){
                        First obj = new Second();
                        obj.m1();
                        obj.m2();
            }
}
Upcasting: object reference of implemented class storing into Interface type variable

Relations:

Scroll to Top