• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Difference between interface and abstract class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between interface and abstrct classes?
If interface can be used instead of abstract classes,why abstract classes are provided in java?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract class is like any other class except that at least one of its methods is declared abstract to indicate that it is not implemented in the class. With that in mind, if you compare a normal class with an interface you will see a vast difference between the two.
-Barry
[ August 31, 2002: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch Rahul.
Bruce Eckel recommends using an interface, instead of using an abstract class, whenever some common behaviour in the base class isn't needed.
It's possible to simulate multiple inheritance with interfaces, but not with abstract classes.
A reason to make abstract a class that hasn't got abstract methods, is to make explicit that it cannot be instantiated.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry is almost correct. Abstract classes do not have to have any abstract methods. The Java API has several abstract classes without abstract methods. You make a class abstract when instantiating it doesn't make any sense but instantiating a child of the class does make sense.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract class without at least one abstract method :roll: ! I thought you were pulling my leg, but you are correct.

does in fact compile.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take it to other extreeme. What if all methods of an abstract class are abstract. Is it now as good as an interface? Not quite so. I can think of following differences:
1. all class level variables in an interface are automatically final and static but in case of abstract class, you will have to explicitly declare them static and final. But that gives you some flexibility i.e. you can have some static, final var and others non-static, non-final at that level of abstractation.
2. any implementing class will be limited to this (abstract) class as a base class. Alternatively, if these abstract methods are implemented in an interface, the implementing class can draw from more than one class and still have opportunity to extend another class.
Thanks
Barkat
 
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
The MouseAdapter class from the java.awt.event package is an example of an abstract class with no abstract methods:
http://java.sun.com/j2se/1.4/docs/api/java/awt/event/MouseAdapter.html
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, see this Javaworld article
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The MouseAdapter is a good example, Thomas, I never noticed that it was abstract; I just remembered that it had methods to be overridden. When meeting java's abstract modifier during an introduction to java, I took note of the fact that if a method is declared as abstract then the class had to be declared abstract. That seemed to be a bit redundant at the time. Now it makes a little more sense.
-Barry
[ September 01, 2002: Message edited by: Barry Gaunt ]
 
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
Here is a recent interesting discussion about the same topic:
https://coderanch.com/t/238777/java-programmer-SCJP/certification/difference-between-interface-abstract-classes
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic