This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
What are the adavantages of using interfaces.In most places it is given as it is instead of multiple inheritance .
My doubt is that as interfaces are 100% abstract classes and have only method delceration and as the implementing classes needs to define them what advantage does the use of interfaces give,,,
ie interface myiface
myclass implements myiface
public void myfunction()
Syst.out.print("I am implementation 1") ;
myclass2 implements myiface
public void myfunction()
Syst.out.print("I am implementation 2") ;
so what difference does the presence of interface makes here...even if interface was not here i have to define the method in functions as needed..So what is the role interfaces have to play.
A table, a chair, a bowl of fruit and a violin; what else does a man need to be happy?:Einstein
Interfaces play a great role in design patterns. Say in you have lot of animal. But all the animals cannot be pets. Only some animals can be pets. So you design a pet interface and use that function in only those animals who are fit to be pets. So pet would be an interface. Also we can implement many specific behaviours using interfaces. Interface is more like a protocol which can be used by anything suitable to use it.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Interfaces play a significant role in decoupling your code. By using interfaces your code isn't so strongly bound to a particular implementation. That way you can upgrade parts of it without chaning a lot. For example on line credit card charges. The overall process is similar, get the info, send it to the bank and process a response.
Now it turns out that Visa and MasterCard are processed by one bank and AmEx by another. So you need two versions of the same thing. You could do it by abstract classes, but its better to use interfaces. You declare the common interface between the two and then create two separate classes that handle the differences in system settings, variables and protocols between both banks. Now your two implementations are interchangeable. So while the initialization process is different depending on the card. The usage is the same and that part of your shopping cart doesn't need wild if/then elses or whatever.
But you say, hey I could have done that with abstract classes too. Yea, but then comes along PayPal and your have to use some API. You can't simultaneously extend both your payment abstract class and PayPal's API to create a payment class that fits in your shopping cart. But you can extend PayPal's API and implement your payment interface. Thus this new PayPal payment class is usable in your shopping cart without having to change your cart's internals. That's the type of benefits you can achieve with interfaces that are not achievable with abstract classes.
@Gerardo: "You can't simultaneously extend both your payment abstract class and PayPal's API to create a payment class that fits in your shopping cart. But you can extend PayPal's API and implement your payment interface" . I do not get why it was decided in the java design that there should not be multiple class extensions? What was teh problem they were trying to address and how does interface solve it?
lakshmi raghavan wrote:
What was teh problem they were trying to address and how does interface solve it?
One problem with multiple inheritance is that a method may be declared and implemented in more than one base class - so which implementation should your class inherit?
Joined: Feb 08, 2005
lakshmi raghavan wrote: I do not get why it was decided in the java design that there should not be multiple class extensions? What was teh problem they were trying to address and how does interface solve it?
Like John points out. If two classes have one or more methods with the same exact signature and you extend both. Which would be called? And if said method is common to both classes why don't they simply extend a base class that has it? And if your class needs functionality from two different classes in a way that it needs to extend both. Then maybe you're on a path to creating a super-class. I'm exagerating here, but consider an example in which you'd want a class to simultaneously be an extension of an email client and pdf generator. Or a dog bean being simultaneously a cat bean because they both walk.
There is also a work around to this. You can have two classes with the required functionality inside your class and then create delegate methods to them. So if you want email and PDF functionality in one. You'd create a class PdfMailer that is neither a PDF generator nor an emailer, but that can email a PDF.
IMHO this lack of multiple class extensions makes thing simpler. Which leads to simpler designs. Which I find better than overly complex ones.
Joined: Jul 09, 2009
Thank You all for your responses
laksmi ragahav wote " I do not get why it was decided in the java design that there should not be multiple class extensions? What was the problem they were trying to address and how does interface solve it?"
one reason for using ifaces is to solve the classic" Diamond problem" .Its another way of naming what Gerardo Tasistro said...
suppose a super class A is there , and two child class that extends from it let it be B,C and let there be another class D which inherits from both B,C. Imagine a diamond structure with A as top corner B,C as other corners below it and D as the bottom most corner .hence this name
So if A contains method loveme() ....B,C will inherit it and override if necessary and when again D inherits from B,C and calls the same function it will be in confusion which one to use. This also happens with variables too
Though this is one usage of interface actual usage of interfaces comes in the case of Polymorphism.Expect Expert comments on this.
Another doubt of mine is all classes defaultly extend the class object, other than that they are allowed to inherit from one more class . So can we say that multiple inheritence is there ,to a maximum from two classes one being implicit always ie the Object class .