• 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

Interface

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey i am not getting the concept of interfaces.I know they are used to implement multiple inheritances.I also know the example that we create an interface car with certain methods so that a class like bmw which implements the car interface has to implement these methods.But I don't know how interfaces come handy?I don't know the meaning of a class calls a method using an interface?(i know that an interface can not be intantiated
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit Guptaaaaa wrote:Hey i am not getting the concept of interfaces.I know they are used to implement multiple inheritances.


True, but that's not the main reason they exist.

I don't know the meaning of a class calls a method using an interface?


Me neither. A class implements an interface, and that means that it must provide actual code for all methods defined in the interface.

It doesn't "call a method using an interface", except in the sense that you can declare a variable as an interface type, and then call one of the interface's methods on that variable. But that's no different to declaring a variable as a supertype of the class that's actually used to initialize it, viz:
private Bmw bmw = new BmwX3();

An interface is simply a supertype of all classes that implement it.

I also know the example that we create an interface car with certain methods so that a class like bmw which implements the car interface has to implement these methods. But I don't know how interfaces come handy?


Because interfaces are proper types, so you can use them any place you might otherwise use a class to declare a type.

Take the following declaration:

private ArrayList<String> list = new ArrayList<String>();

In this case, our code is now tied to an implementation (ie, a class - ArrayList), so if we use that type in a hundred different places in our program, and later on discover that ArrayList was the wrong choice - for example, we work out that it would have been much faster if we'd defined 'list' as a LinkedList - we now have to change our program in 100 different places.

But if we change that definition to:

private List<String> list = new ArrayList<String>();

and use the interface type in the other 100 places, we can change our entire program to use a LinkedList with ONE change, viz:

private List<String> list = new LinkedList<String>();

Hope that helps to explain why interfaces are so useful. You might also want to read up a bit on "programming to the interface".

Winston
 
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is going to a very vague example.
Lets not go into methods and stuffs as of now.

You call Maruthi-Suzuki and tell them that you want a vehicle. The manager agrees. You walk in the next day and you can pick up a sedan, a hatchback, or a gypsy, or even those Ertiga. So the car becomes your interface.

So Car is your interface.
You go and buy a swift-dezire (implement a swift-dezire).
Now Swift-Dezire is your Implementation (implementation is also a class).

On the other hand, you called the manager and asked him for a Swift-Dezire. So you walk in the next day, and you cant really pick up any other car, because you specifically asked for a Swift-Dezire.

Now we will consider a method. fourWheels();

all the cars have to have 4 wheels, rite?? So every implementation has to implement that function.

Did i really sound that stupid??
 
Sooraj Rajagopalan
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston's example of interfaces coming handy by quoting Collections is arguably the best there is. But it will take some time to get used to the concept.

I have no idea why i am mentioning this, but i think i should tell you that an interface is not an intelligent selection statement like Switch. I spent a lot of time trying to figure out where and how and what decides which implementation is implemented. But Interface is one of the most brilliant stuff there is in java.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic