• 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

difference between interface and abstract classes

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anyone please tell me the difference between interface and abstract classes if my abstract class contains only abstract methods?
Is it something like interfaces are used to implement multiple inheritance?
Thanks,
Sachin
 
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
abstract classes may also contain constructors which interface can't !!
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is it something like interfaces are used to implement multiple inheritance?


A class can implement more than one interface in contrast to a class which can inherit only a single superclass(abstract or otherwise)
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) When you declare an interface, the name of the interface is preceded by the keyword interface, it should not be declared abstract. You must use abstract keyword to declare an abstract class.
2) The methods in an interface are not implemented just like abstract methods in abstract class, but they can't be declared abstract.
3)An interface can extends several interfaces(multiple interface hierarchy), a class(including abstract class) can extends only one another class(linear implementation inheritance), implements interfaces(multiple interface inheritance).
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin,
Abstract classes can have constructors.I have a doubt here.It is also true that abstract classes can't be instantiated,that is if a class is declared as abstract no object of that class can be created...Then what is the use of having constructors in abstract classes?
Veena
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe for the above?
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Thomas...
Veena
 
Sachin Choudhari
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply.
Sachin
 
Valentin Crettaz
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 thing, as Thomas pointed out, classes (abstract or not) have a state (instance variables). Interfaces are only allowed to declare constants (static final). Whenever you are in doubt whether you should take an interface or abstract class approach, just ask yourself whether your type under design should be able to hold some state. If the answer is yes, then the abstract class is the correct choice, otherwise an interface could be just fine.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THE DIFFERENCE IS SIMPLE ABSTRACT CLASS CAN HAVE
NON ABSTRACT METHODS BUT INTERFACE ONLY HAVE ABSTRACT METHODS BY DEFAULT.
 
Sachin Choudhari
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks valentin.
I got more clear picture when to use interface and abstract class.
thanks once again.
 
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
And don't forget, an abstract class doesn't have to have any abstract methods.
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
July's JavaPro published James Cooper's review for "Design Patterns Java Workbook", and the very question happened to be one of the "challenges": "Write down three differences between abstract classes and interfaces in Java".
Here is what James Cooper came up with:
"1) Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code.
2) A new class can inherit from an abstract class, but not also from another class. A new class can inherit from any class and still implement one or more interfaces
3) Interfaces do not specify constructors."
And here is what the book lists:
"1) a class can implement any number of interfaces, but subclass at most one abstract class.
2) an abstract class can have nonabstract methods. All methods of an interface are abstract.
3) An abstract class can have instance variables. An interface cannot.
4) An abstract class can define constructor. An interface cannot.
5) An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package).
6) An abstract class inherits from Object and includes methods such as clone() and equals().
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mapraputa Is:
July's JavaPro published James Cooper's review for "Design Patterns Java Workbook", and the very question happened to be one of the "challenges": "Write down three differences between abstract classes and interfaces in Java".
Here is what James Cooper came up with:
"1) Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code.
2) A new class can inherit from an abstract class, but not also from another class. A new class can inherit from any class and still implement one or more interfaces
3) Interfaces do not specify constructors."
And here is what the book lists:
"1) a class can implement any number of interfaces, but subclass at most one abstract class.
2) an abstract class can have nonabstract methods. All methods of an interface are abstract.
3) An abstract class can have instance variables. An interface cannot.
4) An abstract class can define constructor. An interface cannot.
5) An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package).
6) An abstract class inherits from Object and includes methods such as clone() and equals().



I believe there are some words missing from item five. A top-level class or non-nested class may be declared with public or package access, but not protected or private. I believe item five should say that the methods of an abstract class (not the class itself) might have any visibility.
The comments about the visibility of methods declared within an interface are also not correct. The methods of an interface are always implicitly public and abstract even if the modifiers are omitted. Therefore, it is not possible to declare a non-public method in an interface. Although the compiler will accept method declarations within an interface with or without the public modifier, the presence or absence of the modifier makes no difference.
 
reply
    Bookmark Topic Watch Topic
  • New Topic