File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Interfaces in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces in java" Watch "Interfaces in java" New topic
Author

Interfaces in java

Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
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
{
void myfunction();

}

even if
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
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Hi Abhi,

Iterfaces is the way to encapsulate particular kind of behaviour's declaration and forces client
to create the object of the class, implemented this interface.

This is the simplest way to define run time behaviour.

If you want to learn it more, please go through the design pattern basically strategy patten.

I would advice that follow head first design pattern, you ll surely get your answer with some visual examples.

Happy reading,
Tanzy.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
Hai tanzy
ThanKyou
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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.
lakshmi raghavan
Greenhorn

Joined: Aug 23, 2009
Posts: 2
@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?
John Bengler
Ranch Hand

Joined: Feb 12, 2009
Posts: 133
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?
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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.
Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
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

A

B C


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

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

Abhi,
Please go easy on the color.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces in java
 
Similar Threads
Requirement of Interface in Java
NX: Remote/IOException in DBServices interface (Ken...?)
A screen manager?
Abstract and Interface
Aspectj and Spring - adding methods