Write a java program to illustrate the interfaces concept.
Program Algorithm:
Step 1: Create a class with the main function
Step 2: Create an interface ‘Show' with an abstract method. Create an interface ‘Greater' which extends this interface.
Step 3: Create a class'Doo' which implements this interface and it also defines all the abstract methods of this interface.
Step 4: Create an object of this class and call these redefined methods.
Program Code:
import java.io.*;
class Cv
{
public static void main(String args[])throws IOException
{
Doo d=new Doo();
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter value of x:");
d.x=Integer.parseInt(b.readLine());
System.out.println("Enter value of y:");
d.y=Integer.parseInt(b.readLine());
System.out.println("Output:");
d.print();
d.check();
}
}
class Doo implements Greater
{
int x;
int y;
public void print()
{
System.out.println("x:"+x+"\ny:"+y);
}
public void check()
{
System.out.println("Greater out of x and y:"+((x>y)?x:y));
}
}
interface Show
{
void print();
}
interface Greater extends Show
{
void check();
}
Sample Inputs and Outputs:
Enter value of x:
21
Enter value of y:
2
Output:
x:21
y:2
Greater out of x and y :21
Post A Comment:
0 comments: