• 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 and Abstract

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain the exact differnce between Interface and Abstract, other than the explanation like method declaration and object instance. Also please let me get the idea , when we go for interface and when for Abstract?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, there isn't much of a difference between the two, at least as far as I see it. I'm sure there are quite a few technical differences, though. The main difference is that you can only extend ONE class (whether it is abstract or not). However, you can implement as many interfaces as you want. Interfaces are Java's way of allowing a form of multiple inheritance.
Most of the interfaces I use in my code are from the Java API. I seldom create my own interfaces. As noted above, these rare occasions are usually where I need to inherit from two different classes. I think the Java API gives some great examples for when interfaces are appropriate. As you become more familiar with the API, you will see when you should use them in your own code.
HTH
Layne
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract class can have non-abstract methods. An interface can only have abstract methods.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy -- if you consider this more as a "inheritance vs. interface" question, it might help to think about polymorphism!!
I'm using the term polymorphism, in this context, to mean, "The ability to refer to an object in different ways" (in other words, using different *reference variable types*).
Dog d = new Dog(); // treat Dog like a Dog
But if you say:
Animal a = new Dog(); // treat Dog like an Animal
(assume Dog extends Animal)
This lets you send a Dog into any method that uses Animal as its declared argument type.
class Vet {
public void giveShot(Animal a) {
// can take a Dog or Cat or Horse or...
}
}
You can probably imagine that for most applications, the Animal class would be abstract.
But what if you have a class that wants to take Pet objects in its methods... including those that are NOT necessarily subclasses of Animal? Like, what if you have a VirtualPet? (like that Sony robot Dog, or a Tomogatchi (forgive my spelling).
In that case, you would need methods that use polymorphic argument types that are INTERFACES rather than classes.
class PetShop {
public void addPet(Pet p) {
// takes anything that implements Pet, regardless of the inheritance tree that thing comes from)
}
}

interface Pet { ... }
class Dog extends Animal implements Pet { ... }
class RoboDog extends Robot implements Pet { ...}
class VirtualCat extends Simulation implements Pet { ... }
So with abstract classes, you are limiting your use of polymorphism to things that are within the same (and only one) inheritance tree. With interfaces, you can use polymorphism to include things that are from DIFFERENT inheritance trees.
I like to think of inheritance as defining what you ARE. Interfaces, on the other hand, define what you CAN DO.
Like the different hats you can wear.
You can see how important that might be for things like Serializable, where nearly ANY class in Java might (and usually SHOULD) implement Serializable.
Technically, as Thomas and others pointed out, abstract classes can have non-abstract implementations where interfaces cannot. But this is not an issue in most designs, because your interface methods would nearly always need to be overridden anyway, in order to make any sense. For example, if the Pet interface has a beFriendly() method, the way a Dog, Cat, Horse, and VirtualPet demonstrate 'friendliness" is certainly going to be unique to that particular Animal (or Robot) type.
Cheers
Kathy extends Human implements Cowgirl, IceCreamEater, Runnable, Sleepable
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
This is a good question. I had an interview last week and to this question I answer what Layne did and the interviewer didn't seem enough pleased. He said it wasn't what he was looking for and then started to talk about hierarchies. Maybe he was talking about the uses of one over the other, still not a clear issue. In my work projects, the senior programmers decided to use many abstract classes. It worked fine but I rarely saw any interfaces?? I wonder why? Maybe the interviewer was looking for answers that were more practical issues. I believe this is what we need to brush on.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like Kathy's explanation. It gives more details to what I meant about multiple inheritence.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I used Laynes explanation once in an interview - and it was excactly what the interviewer wanted to hear...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is a class called X. If X is having atleast one abstract method, then that class X must be declared as an abstract class. If any class is subclass of X, then that sub class must define that abstract method (giving implementation details) in that sub class. Otherwise that class should also be declared as an abstact class.
If you declare a class with all abstract methods (totally of abstract methods), then you must declare that class as an interface. Those class which declare subclass of this interface, must give implementation details of all the methods declared in interface.
When to use extend or implement.
You can extend only one class but you can implement as many as you want. Only abstract classes are extended. Interfaces are always implemented. But you can extend interface also.
Any corrections in my opinion...
welcome all coments.....
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's really a very good question..which one to use, Interface or Abstract class?
I think, reusablity is the main criterion to go for Interface or Abstract class. Suppose, for u'r given problem, u have sorted out all the functionalities and attributes, if some of the functionalities are common for all the implementing classes and some of them are specific to that particular class. Then obviously, u should go for abstract class and define all those functionalities as abstract methods.
If all the functionalities of the given problem are common for all implementing classes, then use Interface and implement that in u'r classes.
This is what I think...
---------
Nayan.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyothi prakash alluri:
If you declare a class with all abstract methods (totally of abstract methods), then you must declare that class as an interface.


No, you don't have to - it's totally legal to have a "purely abstract class".
It most probably would be a good idea to make it an interface, though...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyothi prakash alluri:
If there is a class called X. If X is having atleast one abstract method, then that class X must be declared as an abstract class.


You can also declare a class as abstract without it having any abstract method. You can do this to prevent this class from being instanciated.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyothi prakash alluri:
If you declare a class with all abstract methods (totally of abstract methods), then you must declare that class as an interface.


This is not true. You can declare the class as abstract. The only restriction is that if you declare one (or more) method(s) abstract then the class MUST be abstract. This is the only condition the compiler will check for (in the context of this discussion), yet there are many other possibilities where you can use the abstract keyword. A class with all abstract methods can be either abstract or an interface. Also, an abstract class can be declared abstract even if none of the methods are abstract.
Keep coding!
Layne
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
You can also declare a class as abstract without it having any abstract method. You can do this to prevent this class from being instanciated.


For those who doubt that you would ever do this, check the Adapter classes that are part of the AWT event model. They are all abstract even though they have no abstract methods.
 
chad stevens
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas, you have a valid point. However, then why would you have interfaces if you can get away with abstract classes shown as in your wrapper class example? The answer to that question would probably clear things up for many of us. Like I said, during my interview the person was babbling on about some hierarchy in abstract classes you can't achieve with interfaces. I am not sure I heard right, his accent was too heavy and his english not too good!
Maybe I should state, in which practical example would you use abstract over interface and vice-versa and WHY? I would like to know the answer to this with real life examples. That would be great!
 
K Elangovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation Kathy Sierra. Happy and thanks for wonderful sharing.
 
K Elangovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation Kathy Sierra. Happy and thanks for wonderful sharing.
Cheers,
K.Elangovan
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface is primarily the replacement of Multiple inheritance in Java.
-------------
Nayan
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface is used to describe what methods a hierarchy of classes should have without describing how those methods should actually work. An abstract class is normally used when you know of some functionality but not all.
Let's take a look at the Collection hierarchy for a rela live implementation.
The Collection interface describes the methods that all Collection objects should have. These include:
boolean add(Object)
void clear( )
boolean contains(Object)
boolean isEmpty( )
Iterator iterator( )
boolean remove(Object)
int size( )
Object[] toArray( )
Any class that implements the Collection interface must have these methods.
The AbstractCollection class implements this interface and provides a fairly basic implementation of the Collection interface without the iterator() and size() methods.
The AbstractList class implements the List interface and extends the AbstractCollection class. It provides a basic implementation of a List. You only need to provide a get() and size() method.
So the interfaces provide the description of the methods and the abstract classes provide partial implementation of the interfaces.
 
chad stevens
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul that is a great explanation, thank you!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another discussion we had in the programmer certification forum about that issue:
difference between interface and abstract classes
 
reply
    Bookmark Topic Watch Topic
  • New Topic