I am aware that java does not support multiple inheritance. And I have heard people saying , multiple inheritance can be achieved using the concept of interface. But I am not able to understand how they are achieved using interface ? So does java really support multiple inheritance or not? When there is a need for an interface to extend more than 2 interfaces ? And why do we need interface first of all ? -raja
hi, For your question why do we need interface first of all ? Say for example you are coding a billing application for an Internet Service Provider.It calculates the following set of charges : a. Joining fee b. Monthly usage c. Excess Consumption Say for example you have three Classes for the calculation. a.JoiningFeeCalculator b.UsageCalculator c.ExcessConsumptionCalculator What is the objective of all these classes ? They have to calculate something...right So we implement a generic command interface called a Calculator interface which will have a calculate method and the interface will be like
and the class will be
So in order to enforce certain methods that is common to all classes we need to implement interface. I think u understand what for interface is used by now . [Note :I have taken part of the example from javaworld.com and i give due credits to the author of the article Ricky yim] For your second question 2. When there is a need for an interface to extend more than 2 interfaces ? I would like to take up the Marker interface example for your second question By the way a marker interface is an interface that doesn't actually define any fields. It is just used to "mark" Java classes which support a certain capability -- the class marks itself as implementing the interface. For example, the java.lang.Cloneable interface.
The following example has three interface contract,Abelian,TypeSafeEnum and a class AddrType which implements the TypeSafeEnum interface. The overall objective of this example is when u compile and generate the javadoc it will emphasize on following the
"equals()","hashcode()" and "toString()" java contracts.
If you note the TypeSafeEnum interface extends the Contract,Abelian interfaces [Note : I had taken this example from www.developer.com from Maximize your Design ROI with Marker Interfaces and JavaDoc By Bruce Wallace and i give due credit to the author]
I hope i clarified both your doubts Regards .PAL.
Added some code tags - Barry [ April 12, 2004: Message edited by: Barry Gaunt ]
SCJP SCWCD SCBCD <br /> <br />what u do in life echos in eternity!
This is a duplicate post. Round these parts, posting the same question in multiple forums is bad practice. It can make it rather difficult to follow a conversation, plus folks spend time saying things that were just said. Let's continue this in the Beginners forum...