Java – Interface before JDK8

Interface (before JDK7):

  • Interface is a collection of abstract methods.
  • Interface methods are by default public and abstract.
  • Any class can implement interface
  • Implemented class override abstract methods.

Note: The object address of implemented class assign to Interface type reference variable.

InterfaceName obj = new ImplementedClass(); 
interface Test
{
            void m1(); // public abstract
}
class Demo implements Test
{
            public void m1()
            {
                        System.out.println(“m1…”);
            }
}
class Main
{
            public static void main(String[] args)
            {
                        Test obj = new Demo();  // upcasting
                        obj.m1();
            }
}
  • Interface variables are by default public static final.
  • We must initialize variables defined in interface.
  • We cannot modify the variables as they are final.
interface Code
{
            int a = 10;  // public static final
}
class Main
{
            public static void main(String[] args)
            {
                        System.out.println(“a value : ” + Code.a);
                        Code.a = 20 ; // Error : final variable cannot be modified
            }
}
Scroll to Top