GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 668 of 921    Print  
When to use Interface over abstract class?
Abstract Classes: Classes which cannot be instantiated. This means one
cannot make a object of this class or in other way cannot create
object by saying  ClassAbs abs = new ClassAbs(); where ClassAbs is
abstract class.

Abstarct classes contains have one or more abstarct methods, ie method
body only no implementation.

Interfaces: These are same as abstract classes only difference is we
an only define method defination and no implementation.

When to use wot depends on various reasons. One being design choice.

One reason for using abstarct classes is we can code common
functionality and force our developer to use it. I can have a complete
class but I can still mark the class as abstract.
Developing by interface helps in object based communication.




  
Total Answers and Comments: 10 Last Update: June 03, 2008   
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
December 15, 2005 09:13:18   #1  
g.soma shekar        

RE: When to use Interface over abstract class?

abstract classes are designed with implemantion gaps for sub-class to fill in.

interfaces are sintacticlly similar to classes but they lack insance variables & methods.

abstract classes can also have both abstract methods & non-abstract methods. where as in interface methods are abstract only, & variables are implicitly static&final.


 
Is this answer useful? Yes | No
January 09, 2007 04:46:33   #2  
umesh choubey        

RE: When to use Interface over abstract class?

hai ,

 when u want to get the sub class(type) property into the super class(type),that we have to achive by using abstact class ,interface is one design we can se.

 other wise both think work same

 Thank & Regards

 umesh choubey


 
Is this answer useful? Yes | No
May 10, 2007 10:46:07   #3  
adarsh83 Member Since: January 2007   Contribution: 1    

RE: When to use Interface over abstract class?
When we do not want any one to create object of our class we define the class as abstract.
when we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will.

thank you

 
Is this answer useful? Yes | No
July 08, 2007 06:39:20   #4  
Sunny        

RE: When to use Interface over abstract class?
Every body is just explaning what is that........ but what about how is that?
 
Is this answer useful? Yes | No
October 15, 2007 02:51:10   #5  
Bharath        

RE: When to use Interface over abstract class?
Abstract classes can perform the same operations as interfaces. But if we want to make a compulsory implementation of the functions, we have to use abstract class.

Also, use interfaces when you want to allow the user to inherit another class and use this as a sub-type.

 
Is this answer useful? Yes | No
March 13, 2008 07:01:15   #6  
omkarpatil83 Member Since: March 2008   Contribution: 1    

RE: When to use Interface over abstract class?
We will use abstract class when we want that the parent class must implement some default behavoiur and keep some behavoiur to be imlemented by parent Class
 
Is this answer useful? Yes | No
May 29, 2008 17:13:18   #7  
Tins30 Member Since: May 2008   Contribution: 3    

RE: When to use Interface over abstract class?

Interface provides method signature and not the body. Its up to the developers to
provide body for these methods once they implement it.

Abstract provides both, methods with and without body. Adding new methods in abstract class in middle of development life cycle won't break existing sub classes unless these methods are declared as mustoverride. If there are frequent addition of new methods, properties and so on., one should use abstract..

Adding new methods to the interface will force users to go through each and
every sub class that implemented that interface and call these methods.
So its always preferred to use abstract over interface because of this limitation.

Whereas on the other hand interface can fill gap of multiple inheritance. One can
implement from more than one interface. Not same with Abstract. One can inherit
from only one abstract class.

If you want to inherit from class that can provide reusable code then use abstract class. For instance, connecting to the database or base class for grid or treeview.
If you implement your subclass from an interface then in each subclass you will have to write your own code to fill body of these methods.

Hope this helps.

Thanx
Tins30


 
Is this answer useful? Yes | No
May 30, 2008 09:05:38   #8  
Tins30 Member Since: May 2008   Contribution: 3    

RE: When to use Interface over abstract class?
"adarsh83 wrote:

When we do not want any one to create object of our class we define the class as abstract.
when we have some properties to which the functionalities that defer depending on the user specifications we use interfaces where the common properties are declared but the implementation is done by the user according to his will."

That will still leave me wondering... When would someone not want to create object of some class? You also mentioned about when to define an interface.. But same is true for abstract class.. We can use abstract class to do exactly same thing that you
stated for interface.. Plz correct me if I am wrong.

Thanx
Tins30

 
Is this answer useful? Yes | No
June 01, 2008 15:33:27   #9  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: When to use Interface over abstract class?
Interface:
1. If u want to implement all of the methods than we go for interface.
2. In future to put any methods that dows not effect.
3. To implemnting Inheritence Future

Abstarct:

1. when it necessay dont create the instance of the class for the security purpose.

 
Is this answer useful? Yes | No
June 03, 2008 09:10:28   #10  
Tins30 Member Since: May 2008   Contribution: 3    

RE: When to use Interface over abstract class?
Karuna Reddy wrote:

"
Interface:

1. If u want to implement all of the methods than we go for interface.
2. In future to put any methods that dows not effect.
3. To implemnting Inheritence Future

Abstarct:

1. when it necessay dont create the instance of the class for the security purpose. "

Underneath Interface:

Point 1. Right

Point 2. I believe this is not truly accurate.. Not trying to be instigator!!

If you want to put any methods that does not affect(I believe you meant that does not affect other class) then I would use abstract class not interface. Here's an explanation why? For instance, if you are already implementing from an interface in 10 different sub classes and if you add another method to the interface then you will have to go through each and every sub class and declare this method. Else you will get an error. So I believe point 2 should be under Abstract. Thats why I always prefer abstract over interface. Interface is bit more strict on implementation than abstract on inheritance.

Point 3. Interface supports multiple inheritance but C# does not truly support multiple inheritance like C++ and so one can use interface and can also implement from more than 1 interface.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape