Interface

Keyword "interface" is used to define this class.
Keyword "implemets" is used to inherit the interface into classes.
interface xyz { public void fun1(); public void fun2(); } class A implements xyz { public void fun1() { System.out.println("Hello Function 1"); } public void fun2() { System.out.println("Hello Function 2"); } public void fun3() { System.out.println("Hello Function 3"); } public static void main(String args[]) { A a1=new A(); a1.fun1(); a1.fun2(); a1.fun3(); } }
Example :
Class A implements xyz, pqr, abc.
It look like multiple inheritance but it is not actually.
Final keyword :
Keyword "final" is used to create constant in Java program. The values of these variables are never changed at runtime.Example :
final int x=3;
x=5; // error
x=3; // error
x++; // error
interface ABC { public final static int x=10; int y=20; } class B implements ABC { public static void main(String args[]) { B b1=new B(); System.out.println("X is "+b1.x); System.out.println("Y is "+b1.y); // b1.X=50; error // b1.Y=100; error System.out.println("X is "+ABC.x); System.out.println("Y is "+ABC.y); } }
Post A Comment: