• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces in java

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abhi Venu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai tanzy
ThanKyou
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi,
Please go easy on the color.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic