What Is an Interface?
In general, an interface is a device or a system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks.
Within the Java programming language, an interface is a type, just as a class is a type. Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.
The bicycle class and its class hierarchy define what a bicycle can and cannot do in terms of its "bicycleness." But bicycles interact with the world on other terms. For example, a bicycle in a store could be managed by an inventory program. An inventory program doesn’t care what class of items it manages, as long as each item provides certain information, such as price and tracking number. Instead of forcing class relationships on otherwise unrelated items, the inventory program sets up a protocol of communication. This protocol comes in the form of a set of method definitions contained within an interface. The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on.
To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface. Thus, the bicycle class would provide the implementations for the methods that set and get retail price, assign a tracking number, and so on.
You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following:
- Capturing similarities among unrelated classes without artificially forcing a class relationship
- Declaring methods that one or more classes are expected to implement
- Revealing an object's programming interface without revealing its class
- Modelling multiple inheritance, a feature that some object-oriented languages support that allows a class to have more than one superclass
Simple Program On Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle
/* Area Of Rectangle and Triangle using Interface * /
interface Area
{
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
/** OUTPUT **
Area Of Rectangle = 2.0
Area Of Triangle = 10.0
*/
hi....i m javith.this post helped me very much....thanks to all your team...specially for "u"..
and a small request.write more programs like this....
Hi,
This program is very simple and understandable. I understood Interface concept very easily and found very helpful. Thanks a lot.
Regards,
Seetaram
hi can anyone tell why we are declaring area as a variable for AREA interface??
I like that interface example (remote and person)...keep it up
hi i think this eg is for single inheritencce.Multiple inheritence means producing subclasses from two or more superclasses right!
hi..we are use the area as varible to the Area because area is a referance varible to the Area to call the compute method...
hi..we are use the area as varible to the Area because area is a referance varible to the Area to call the compute method...
these example
java programs
make explanation more simple
this is wrong
how does it provide multiple inheritance as both classes are implementing a single interface.this could also be achieved by abstract class
/* write a java program using multiple inheritance to find area of a rectangle and triangle */
class dc //base class
{
int a,l,b;
void set(int x,int y)
{
l=x;
b=y;
System.out.println("val of l="+l);
System.out.println("val of b="+b);
}
void areaOfRectangle()
{
a=l*b;
}
}//dc
class cd extends dc//derived class
{
int a,h,w;
void set(int x,int y)
{
super.set(x,y);
h=x;
w=y;
System.out.println("val of h="+h);
System.out.println("val of w="+w);
}
void areaOfTriangle()
{
super.areaOfRectangle();
System.out.println("area of rect="+super.a);
this.a=(h*w)/2;
System.out.println("area of triangle="+this.a);
}
}//cd
class idemo6
{
public static void main(String[] args)
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
cd c=new cd();
c.set(x,y);
c.areaOfTriangle();
}
}
simple and crystal clear.i wish even textbooks had such examples.
there is an exception occuring during runtime with this program
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\GLENN>cd desktop
C:\Users\GLENN\Desktop>cd java
C:\Users\GLENN\Desktop\java>javac InterfaceArea.java
C:\Users\GLENN\Desktop\java>java InterfaceArea
Exception in thread "main" java.lang.NoClassDefFoundError: InterfaceArea
C:\Users\GLENN\Desktop\java>
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\GLENN>cd desktop
C:\Users\GLENN\Desktop>cd java
C:\Users\GLENN\Desktop\java>javac InterfaceArea.java
C:\Users\GLENN\Desktop\java>java InterfaceArea
Exception in thread "main" java.lang.NoClassDefFoundError: InterfaceArea
C:\Users\GLENN\Desktop\java>
Hey can you give example of multiple inheritance using interfaces using some different example other than Area of Triangle etc..
I think more convincing reason for not supporting multiple inheritance is complexity involved in constructor chaining, casting etc rather than diamond problem
Javin
Why multiple inheritance is not supported in Java
Thanks
the same above mentioned error is occuring.
yhanks from niteen kitture
great program...tnks a lottttt
USE INDENTATION!!!
thanx a ton !!!!!!!!
Thanx a ton !!
why this kolaveri in java...
pls teach me java...i am a beginer in java...
why this kolaveri in java...
Thanks and Thanks
This can be done through having a super class and then extending this superclass and then overriding the area method...can u give a more necessary usage of interfaces ???
The Program above was all wrong .Program Below was 100% correct....
/* Area Of Rectangle and Triangle using Interface * /
interface Area
{
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class Interface Area
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
System.out.println("Area Of Rectangle = "+ rect.compute(1.0f,2.0f));
System.out.println("Area Of Triangle = "+ tri.compute(10.0f,2.0f));
}
}
/** OUTPUT **
Area Of Rectangle = 2.0
Area Of Triangle = 10.0
*/
The same error may occur plz give the solution
This program is very easy to understand. Thank u so much
hi
hi.... im nedumaran this is very usefull to me lot of thanks