java 4
java 4
1.The members of an
1.The member of a class
can be constant or
interface are always
variables. declared as constant.
2.The class definitions can 2.The methods in an
contain code for each of interface are abstract in
its methods. nature.
3.It can be instainted by 3.It cannot be used to
declaring objects. declare objects, It can
4.It can use various access be only inherit by class.
specifiers like public , 4. It can only use the
private or protected. public access specify.
Here interface is a keyword and
interface name is any valid java
variable .
Syntax:
Interface interfaceName
{
Variable Declaration;
Methods Declaration;
}
Like classes, Interfaces can also be
extended. That is an interface can be
sub interfaced from other interfaces by
using the keyword extends.
Syntax:
Interface name1 extends name2
{
Body of name 2
}
Interfaces are used as “superclasses”
whose properties are inherited by
classes by using the keyword
implements.
Syntax:
Class classname impements
interfacename
{
body of class
}
Interface Area
{
final static float pi=3.14f;
int compute(int x, int y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
Return(x*y);
}}
Class circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class test
{
public static void main(String arg[])
{
Rectangle r1= new Rectangle();
circle c1= new circle();
Area a;
a=r1;
System.out.println(“Area of a rectangle=“+
a.compute(10,20));
a=c1;
System.out.println(“Area of a square=“+
a.compute(10,0));
}
}
interface a // an interface is created
{
Int a=10; //variable declared
}
Interface b
{
String str =new string (“hello”);
}
Interface c extends a, b // interface c inherit a and b interface OUTPUT
{ 10
Void show(); Hello
}
Class test implements c
{
Public void show()
{
System .out. Println (“value of a “+ a) ;
System .out. println (“value of string:” + str) ; } }
Class test
{
Public static void main( String args[])
{
test t1 = new test () ;
t1. show () ;
} }
THANK YOU