Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class and interface

 
Madhawi Maurya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me which one is better either abstract class or interface? which one should use in any appliction program?
 
Sujith Mahesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class can have both abstract methods and non abstract methods.
Interface methods are implicitly "public abstract".
Abstract class can have non static variables.
Interface variables are implicitly "public final static".

Depending on the above inforamtion you can choose which suits best for your application and implement it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sujith mg:
Depending on the above inforamtion you can choose which suits best for your application and implement it.


Well, sort-of, but it omits the most important difference, which is that a class can extend only one superclass (abstract or not), but can implement any number of interfaces.

Interfaces are preferred to abstract classes, in general. Use abstract classes only when an interface cannot give you what you need.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the SCJA exam objectives is "Programming to an Interface."

There is no exam objective called "Programming to an Abstract class."



It all depends upon the use and the scenario, but typically, we see that the greater the deal of 'abstraction' the better, and it doesn't get much more abstract and flexible than an Interface.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic