You are on page 1of 4

Practical_7

1.Write a program that illustrates interface inheritance. Interface K1 declares


method mK and a variable integer variable that is initialized to 1. Interface
K2 extend K1 & declares mK. Interface K3 extends K2 & declares mK. The
return type of mK is void in all interfaces. Class U implements K3. Its version
of mK displays value of integer variable. Instantiate U & invoke its method.
Program:
interface k1
{
int a=1;
void mk();
}
interface k2 extends k1
{
void mk();
}
interface k3 extends k2
{
void mk();
}
class u implements k3
{
public void mk()
{
System.out.println("a="+a);
}
}
public class p7_1
{
public static void main(String[] args)
{
u u1=new u();
u1.mk();
}
}

Output:
a=1

2. Write a program to find out the area of square and circle using interface. Where you
have to take two classes for circle and rectangle and one interface.
Program:
interface figure
{
void area();
}
class circle implements figure
{
int r;
double area;
circle(int r)
{
this.r=r;
}
public void area()
{
area=3.14*r*r;
System.out.println("area of circle is "+area);
}
}
class rectangle implements figure
{
int a,b;
double area;
rectangle(int a,int b)
{
this.a=a;
this.b=b;
}
public void area()
{
area=a*b;
System.out.println("area og rectangle is "+area);
}

}
public class p7_2
{
public static void main(String[] args)
{
circle r1=new circle(2);
rectangle r2=new rectangle(2,2);
r1.area();
r2.area();
}
}
Output:
area of circle is 12.56
area og rectangle is 4.0
3.Write a java program which shows importing of classes from other user define packages.
Program:
package pack2;
public class display
{
public void disp()
{
System.err.println("we are in package 2");
}
}
package pack1;
import pack2.display;
public class abc
{
public static void main(String[] args)
{
display d1=new display();
d1.disp();
}
}

Output:
we are in package 2

4. Write a program that demonstrates use of packages & import statements.


(Simple addition program)
Program:
package pack2;
public class add
{
int a;
int b;
int c;
public add(int a,int b)
{
this.a=a;
this.b=b;
}
public void addition()
{
c=a+b;
System.out.println("answer is "+c);
}
}
package pack1;
import pack2.add;
public class def
{
public static void main(String[] args)
{
add a1=new add(2,2);
a1.addition();
}
}
Output:
answer is 4

You might also like