Interfaces - Java

Using the keyword interface you can fully abstract a class interface from its implementation. Interfaces are syntactically similar to classes but they lack instance variables and their methods are declared without any body. One calss can implement any number of interfaces.

Syntax:-
Access type Interface interface name
{
interface member(Arg list);
- - - -  - -  - - -  -  -  -  -  -  -  -
}                                 

 WRITE A PROGRAM ON INTERFACE.                    

 

interface Abc
{
void put(int x);
}
class Xyz implements Abc
{
public void put(int x)
{
System.out.println("value of x: "+x);
}
}
class Sample
{
public static void main(String args[])
{
Xyz s=new Xyz();
s.put(10);
}
}

 WRITE A PROGRAM TO FIND THE SUM OF TWO NUMBERS USING INTERFACE.

interface Abc
{
void put(int x,int y);
}
class Xyz implements Abc
{
public void put(int x,int y)
{
System.out.println(x+y);
}
}
class Sample
{
public static void main(String args[])
{
Abc x=new Xyz();
x.put(10,20);
}
}

WRITE A PROGRAM TO FIND THE AREA OF TRIANGLE USING REFERENCED OBJECTS.

interface Abc
{
void put(int x,int y);
}

class Pqr implements Abc
{
public void put(int x,int y)
{
System.out.println("Area of triangle: "+(x*y)/2);
}
}
class Sample
{
public static void main(String args[])
{
Abc a=new Xyz();
Pqr p=new Pqr();

                a.put(5,6);
a=p;
a.put(5,8);
}
}

WRITE A PROGRAM TO FIND THE NET AMOUNT OF A PRODUCT USING VARIABLES IN INTERFACE.

interface Abc
{
int ic=10;
int qty=5;
}
class Xyz implements Abc
{
int rate,amt;
Xyz(int x)
{
rate=x;
amt=qty*rate;
}
public void put()
{
System.out.println(ic+" "+amt);
}
}
class Sample
{
public static void main(String rags[])
{
Xyz x=new Xyz(5);
x.put();
}
}

WRITE A PROGRAM TO FIND ALL SIMPLE MATHEMATICAL CALUCLATIONS USING EXTENDS OF INTERFACES.

interface Abc
{
void sub();
}

 

interface Abc1 extends Abc
{
void sub();
}

interface Xyz
{
void mul();
}
interface Xyz1
{
void div();
}

class Pqr implements Abc1
{
public void sum()
{
System.out.println(10+20);
}
public void sub()
{
System.out.println(10-20);
}
}

class Mno extends Pqr implements Xyz,Xyz1
{

        public void mul()
{
System.out.println(20*30);
}
public void div()
{
System.out.println(30/20);
}
}

class Sample
{
public static void main(String args[])
{
  Mno x=new Mno();
x.sum();
x.sub();
x.mul();
x.div();
}
}

For
Online Classes

Contact Us: 9885348743