Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why to go for interface

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, yesterday while i was reading head first java regarding interfaces and abstract classes i got a strange doubt.
i am unable to get it by my own

Here is my simple doubt...

while have an abstract class why should i go for interface..
i think abstract class is far better than interface because we can write both abstract methods and also concrete methods and we can extend the this abstract class and we can inherit the functionalities fo the abstract class by all the classes which extends.
and now comming to interface we can just write the declaration of method we cannot even write the methods with implementaion.. then what is the use of this




please can any one tell me the reason and also one Best REAL TIME example for abstract class and interfaces (in your work have u ever used these things?how?)

thanks to u all in advance

Note: This the question asked by most of the interviewers
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets use the Collections faramework in java.util as an example. Interfaces define behaviour but not implementation. If you expose your code as an interface you are telling people how the related classes behave but you do not enforce any particular implementation.

But as you say, Abstract classes give a good way to introduce basic behaviour which can then be used to provide specific implementations.

Coming back to collections, all operations are defined in terms of the interfaces. This allows the framework to work without tying users to any concrete implementation. But many of the interfaces have common basic behaviour, so the framework also supplies abstract classes which implement the interfaces, which gives both implementation and basic behaviour to make it compatible wit hthe framework.

Does this help?

Dave
[ July 14, 2006: Message edited by: David O'Meara ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, I too want to talk about util package
here collection class is a (parent) interface and need to be implemeted by other abstract classes like abstractcollection,abstractlist,abstractset...

why we need to do this
why can't we directly go for abstract classes..

so that arraylist and hashset can extend the abstraactlist and abstractset and inherit the functionlaities,properties of the abstract class and also implent their own mehtods tooo..
:roll:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the Iterable/Iterator interfaces of the java.util package. You may already know that the List, and Set interfaces extend the Iterable interface which means that ecah class that implments them must provide a method:

Iterator iterator();

But what if you wanted to write your own Iterable implementation. Consider this example that iterates over tokens in a String (applicable in Java 1.5):


My point is iterating over a List or a Set or a String are three entirely different operations that would be difficult to implement from one base class.
[ July 15, 2006: Message edited by: Garrett Rowe ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya iterator needs different implementations for List ,Set,...
but why iterator must be declared as interface why not abstract class?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also:

InterfaceVsAbstractClass

Edit: removed useless link
[ July 15, 2006: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
ya iterator needs different implementations for List ,Set,...
but why iterator must be declared as interface why not abstract class?


It could be declared an abstract class with all abstract methods. But what advantage does that have over an interface?

A single class in Java may implement multiple interfaces, but it can only extend one parent class
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here collection class is a (parent) interface and need to be implemeted by other abstract classes like abstractcollection,abstractlist,abstractset...

why we need to do this
why can't we directly go for abstract classes..


Another option that the interface leaves open is for you to write your own implementation from scratch. Maybe the code provided by Sun in the abstract class just won't work for your custom Collection.

You can also take an existing class that already does some work and add another "implements Xyz" to it. It may not be possible to do this with an abstract class ... if you already extend AbstractWidget you cannot also extend AbstractSet.

Both levels of abstraction are useful. Hope this helps see why they both exist and when you might use one over another.
 
Naseem Khan
Ranch Hand
Posts: 809
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
ya iterator needs different implementations for List ,Set,...
I tried to work out what "ya" means in context, but had to give up. Certainly none of these seem to work:
  • yam
  • yank
  • yankee
  • yap
  • yard
  • yardstick
  • yarn
  • yarrow
  • yawn
  • yawning
  • Instead of just ignoring postings that make no sense, I feel it is better to ask the poster to Use Real Words.

    Regards, Andrew
     
    saikrishna cinux
    Ranch Hand
    Posts: 689
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i am sorry Mr Andrew, i will try to use real words from now, last time i was little bit hurry in my office work, so i could able to concentrate on my words
     
    saikrishna cinux
    Ranch Hand
    Posts: 689
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stan James:


    Another option that the interface leaves open is for you to write your own implementation from scratch. Maybe the code provided by Sun in the abstract class just won't work for your custom Collection.


    yes, u r right but again my doubt is why to go for interfaces, while i have abstract class

    i will just extend abstract class and i will override the method implementation,and if need to have some more methods i will add them too.

    ok now what do u say Mr Stan?

    :roll:

    thanks
    sai
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Garrett Rowe:

    A single class in Java may implement multiple interfaces, but it can only extend one parent class


    And that is in fact the only technical reason for the existence of interfaces in Java.

    In C++, which allows multiple implementation inheritance, you don't need interfaces, because you can just use fully abstract classes.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by saikrishna cinux:

    yes, u r right but again my doubt is why to go for interfaces, while i have abstract class

    i will just extend abstract class and i will override the method implementation,and if need to have some more methods i will add them too.


    Because in Java you are not allowed to write

    class MyList extends AbstractList, AbstractIterable { ...
     
    Vj Kamath
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can implement multiple interfaces. But you cannot extend multiple classes.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic