Interface examples program in Java

Content 


1. Program to use Simple Interface.

interface  AInterface
{
void showA();
}

class Test  implements  AInterface
{
public void showA()
{
System.out.println("showA() of A interface.");
}
}

class  InterfaceTest1
{
public static void main( String args[ ] )
{
Test  t1 = new Test();
t1.showA();

AInterface  a1 = new Test();
a1.showA();

a1 = t1;
a1.showA();
}
}

Output:

showA() of A interface.
showA() of A interface.
showA() of A interface.





2. Program to use field in interface.

interface AInterface
{
int  SIZE = 100;
void  showA();
}

class Test  implements  AInterface
{
public void showA()
{
System.out.println("showA() of A interface.");
System.out.println("SIZE = " + SIZE);
}
}

class  InterfaceTest2
{
public static void main( String args[ ] )
{
AInterface  a1 = new Test();
a1.showA();
}
}

Output:

showA() of A interface.
SIZE = 100




3. Program to explain partial implementation of an interface.

interface AInterface
{
void showA();
void showB();
}

abstract  class  B  implements  AInterface
{
public void showA()
{
System.out.println("showA() of A interface.");
}
}

class Test extends B
{
public void showB()
{
System.out.println("showB() of A interface.");
}
}

class InterfaceTest3
{
public static void main( String args[ ] )
{
AInterface  a1 = new Test();
a1.showA();
a1.showB();
}
}

Output:

showA() of A interface.
showB() of A interface.




4. Program to explain Inheritance of a interface from another interface.

interface AInterface
{
void showA();
}

interface BInterface extends AInterface
{
void showB();
}

class Test  implements BInterface
{
public void showA()
{
System.out.println("showA() of A interface.");
}

public void showB()
{
System.out.println("showB() of B interface.");
}
}

class InterfaceTest4
{
public static void main( String args[ ] )
{
AInterface  a1 = new Test();
a1.showA();

BInterface b1 = new Test();
b1.showA();
b1.showB();
}
}

Output:

showA() of A interface.
showA() of A interface.
showB() of B interface.





5. Program to explain Inheritance of a interface from multiple interfaces.

interface A
{
void showA();
}

interface B
{
void showB();
}

interface C extends  A, B
{
void showC();
}

class  D  implements  C
{
public void showA()
{
System.out.println("showA() of A interface.");
}

public void showB()
{
System.out.println("showB() of B interface.");
}

public void showC()
{
System.out.println("showC() of C interface.");
}
}

class  InterfaceTest5
{
public static void main( String args[ ] )
{
C c1 = new  D();
c1.showA();
c1.showB();
c1.showC();
}
}

Output:

showA() of A interface.
showB() of B interface.
showC() of C interface.




 6. Polymorphism  with interface example

interface Shape
{
void draw();
}

class Rectangle implements Shape
{
public void draw()
{
System.out.println("Easy Programming24");
}
}

class Triangle implements Shape
{
public void draw()
{
System.out.println("All Programs");
}
}

class  PolymorphismTest8
{
public static void main( String args[ ] )
{
Shape s;

Rectangle r = new Rectangle();
s = r;
s.draw();

Triangle t = new Triangle();
s = t;
s.draw();
}
}

Output:

Easy Programming24
All Programs


7. Program to Implement a class from multiple interfaces.

interface  A
{
void showA();
}

interface  B
{
void showB();
}

class  C  implements  A, B
{
public void showA()
{
System.out.println("showA() of A interface.");
}

public void showB()
{
System.out.println("showB() of B interface.");
}
}

class InterfaceTest8
{
public static void main( String args[ ] )
{
C  t = new  C();
t.showA();
t.showB();
}
}

Output:

showA() of A interface.
showB() of B interface.

   


Reactions

Post a Comment

0 Comments