0% found this document useful (0 votes)
3 views

java 4

Uploaded by

Hrutvij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java 4

Uploaded by

Hrutvij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

INTERFACES

Abstract methods are those methods which are to be


redefined in sub class , thus making overriding
compulsary.
Example:
abstract class shape
{
………………….
………………….
abstract void draw(); // prototype
…………………
…………………
}
.
 We cannot use abstract classes to
instantiate objects directly.
 The abstract method of an abstract
class must be defined in its sub class.
 We cannot declare abstract constructor
or abstract methods.
 Multiple inheritance is not possible in java
programming language but we can achieve
the feature through interfaces. An interface
is basically a kind of class. Like class it
contain method and variables with the
difference that an interface can contain
only abstract method and abstract classes.
They cannot contain any code. This is the
responsibility of the class that implements
an interface to define the code for
implementation of these methods.
CLASS INTERFACE

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

You might also like