Skip to main content

Interface

Interface

It is the Collection of public, abstract Methods. (It consists by default)
An interface program can have any number of implements

Example of Interface

package Test;

import java.util.Scanner;

interface Caluclator

{
void mul();
void div();
}


class MyCalculator implements Caluclator
{
public void mul()
{
int a=10;
int b=20;
int c= a*b;
System.out.println(c);
}

public void div()
{
int a=10;
int b=20;
int c= a/b;
System.out.println(c);
}

}

class MyCalculator1 implements Caluclator
{
public void mul()
{
Scanner scan= new Scanner(System.in);

System.out.println("Enter the Value of a");
int a= scan.nextInt();

System.out.println("Enter the Value of b");
int b= scan.nextInt();


int c= a*b;
System.out.println(c);
}

public void div()
{
Scanner Scan= new Scanner(System.in);

System.out.println("Enter the Value of a");
int a=Scan.nextInt();

System.out.println("Enter the Value of b");

int b=Scan.nextInt();
int c= a/b;
System.out.println(c);
}

}
class MyCalculator2 implements Caluclator
{

public void mul()
{
Scanner Scan= new Scanner(System.in);

System.out.println("Enter the Value of a");
int a=Scan.nextInt();
System.out.println("Enter the Value of b");
int b=Scan.nextInt();

if(a==0 || b==0)
{
System.out.println("invalidInput");
}
else
{
int c =a*b;
System.out.println(c);
}
}
public void div()
{
Scanner Scan= new Scanner(System.in);
System.out.println("Enter the Value of a");
int a=Scan.nextInt();
System.out.println("Enter the Value of b");
int b=Scan.nextInt();
if(a==0 || b==0)
{
System.out.println("invalid Input");
}
else
{
int c=a/b;
System.out.println(c);
}
}
}

class Test{
public static void main(String []args)
{
MyCalculator mc1=new  MyCalculator();
MyCalculator1 mc2=new MyCalculator1();
MyCalculator2 mc3=new MyCalculator2();

Math m= new Math();
m.permit(mc1);
m.permit(mc2);
m.permit(mc3);


}

}

class Math{
void permit(Caluclator ref)
{
ref.mul();
ref.div();

}
}

Comments