Multiple Inheritance using Interfaces

33 comments - Post a comment

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
*/

 
This Post has 33 Comments Add your own!
javeedahmed - February 4, 2010 at 9:18 PM

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....

kinda - June 29, 2010 at 11:37 AM

Hi,
This program is very simple and understandable. I understood Interface concept very easily and found very helpful. Thanks a lot.

Regards,
Seetaram

GOWTHAMAN - September 16, 2010 at 12:14 PM

hi can anyone tell why we are declaring area as a variable for AREA interface??

Anonymous - September 24, 2010 at 8:56 PM

I like that interface example (remote and person)...keep it up

praveen - October 2, 2010 at 9:06 PM

hi i think this eg is for single inheritencce.Multiple inheritence means producing subclasses from two or more superclasses right!

Anonymous - October 3, 2010 at 7:28 PM

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...

Anonymous - October 3, 2010 at 7:28 PM

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...

metallic - November 21, 2010 at 11:45 PM

these example
java programs
make explanation more simple

EARN FROM INTERNET - November 27, 2010 at 6:12 AM

this is wrong

Anonymous - November 30, 2010 at 9:06 PM

how does it provide multiple inheritance as both classes are implementing a single interface.this could also be achieved by abstract class

krishna chaitanya - March 3, 2011 at 5:12 PM

/* 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();

}
}

Anonymous - April 8, 2011 at 4:10 PM

simple and crystal clear.i wish even textbooks had such examples.

gareth - April 17, 2011 at 11:04 AM

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>

gareth - April 17, 2011 at 11:05 AM

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>

Sushil - July 9, 2011 at 10:03 PM

Hey can you give example of multiple inheritance using interfaces using some different example other than Area of Triangle etc..

JP@ unsupportedClassversionerror in java - July 14, 2011 at 8:10 PM

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

Anonymous - December 20, 2011 at 9:50 AM

Thanks

pooja - December 20, 2011 at 9:26 PM

the same above mentioned error is occuring.

niteen - January 11, 2012 at 7:50 PM

yhanks from niteen kitture

Anonymous - January 24, 2012 at 9:15 PM

great program...tnks a lottttt

Anonymous - February 16, 2012 at 1:22 PM

USE INDENTATION!!!

Sneha SG - March 14, 2012 at 9:44 PM

thanx a ton !!!!!!!!

Sneha SG - March 14, 2012 at 9:45 PM

Thanx a ton !!

bill gates - April 12, 2012 at 2:58 PM

why this kolaveri in java...

bill gates - April 12, 2012 at 3:00 PM

pls teach me java...i am a beginer in java...

bill gates - April 12, 2012 at 3:00 PM

why this kolaveri in java...

Anonymous - April 30, 2012 at 11:24 PM

Thanks and Thanks

Anonymous - June 17, 2012 at 5:39 PM

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 ???

Anand ( India.Chennai..) - July 12, 2012 at 11:29 PM

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
*/

Anonymous - August 26, 2012 at 10:56 AM

The same error may occur plz give the solution

Favourites - September 20, 2012 at 3:37 PM

This program is very easy to understand. Thank u so much

Anonymous - September 20, 2012 at 3:40 PM

hi

Anonymous - October 8, 2012 at 5:42 PM

hi.... im nedumaran this is very usefull to me lot of thanks

Post a Comment