Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes why to go for interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why to go for interface" Watch "why to go for interface" New topic
Author

why to go for interface

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
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


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Apr 16, 2005
Posts: 689
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

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
ya iterator needs different implementations for List ,Set,...
but why iterator must be declared as interface why not abstract class?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
See also:

InterfaceVsAbstractClass

Edit: removed useless link
[ July 15, 2006: Message edited by: Garrett Rowe ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Interfaces v/s Abstract Class

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

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

    Joined: Apr 16, 2005
    Posts: 689
    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

    Joined: Apr 16, 2005
    Posts: 689
    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

    Joined: Jul 11, 2001
    Posts: 14112
    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.


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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

    Joined: Nov 19, 2004
    Posts: 15
    You can implement multiple interfaces. But you cannot extend multiple classes.
     
     
    subject: why to go for interface