• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

what is the need of interface?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is interface needed in java.... if we say that any class which implememnts a particular interface has the flexibility to define its method in its own way..then we might as well as declare a class with the required method....


why do we need to define an interface.... ???


can anybody give me the basic idea.....


[ November 16, 2007: Message edited by: Bear Bibeault ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the doc for List. This is an interface, so I can write something like:

I never need to know exactly what kind of List somebody calls me with - a LinkedList, Arraylist, etc. You can write a whole new class that implements the List interface and pass it to my method and I'll work just fine.

The next question is probably: How is interface better than abstract class? AbstractList happens to exist in the library so there must be a place for abstract classes here, too. Try the search widget at the top of the page for "asbtract vs interface" in the FAQs.

Does that answer the right question? Raise some new ones?
[ November 16, 2007: Message edited by: Stan James ]
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though it is old question, I would like to give some good answer for this question.

Sun introduced Interface concept to avoid diamond inheritance problem in C++.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rahul mehra:
why is interface needed in java.... if we say that any class which implememnts a particular interface has the flexibility to define its method in its own way..then we might as well as declare a class with the required method....


why do we need to define an interface.... ???


can anybody give me the basic idea.....




In addition to the use within the Java language itself that Stan talks about, interfaces are very useful in another way. A classic example of the power of interfaces is the JDBC (Java Database Connectivity) API. If you look at the API docs for the java.sql package, you will see there are a lot of interfaces defined for which there are no actual implementing classes. It is left to to the database vendor to actually implement the class. In fact, Java itself does not have implementations of these interfaces.

This allows me to write code based on the interface, say a Statement and a ResultSet, that works regardless of the underlying database.



That code is programmed entirely against the interfaces in JDBC. At runtime, I may use the MySQL JDBC JAR file in my classpath which has the ResultSet and Statement implementations for the MySQL database; Or I might use the Apache Derby JAR, or the Hypersonic SQL DB JAR, or Oracle JDBC JAR, etc. It doesn't matter; my code runs the same because I programmed to the interface. This makes the code far more flexible. I can switch the underlying database without having to make any code changes. This prevents me from getting locked into a specific database vendor. (At my old job, when our database vendor was going to significantly raise licensing costs, we simply switched database vendors with no code changes. We could do such because of the JDBC API.) The JDBC API also provides more general flexibility. For example, in my project at work, our test code runs against a very light weight (and free) in memory test database; where as the same code, when in production, runs against a full scale database.

That is very powerful and useful.

p.s. you can read about the Diamond of Death Venkat Sadasivam mentioned at Wikipedia.
[ August 16, 2008: Message edited by: Mark Vedder ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With other words, interfaces allow the use of runtime polymorphism with static typing and without implementation inheritance.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic