GeekInterview.com
Series: Subject: Topic:
Question: 1065 of 2109

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.

Asked by: Interview Candidate | Asked on: Jun 27th, 2005
Showing Answers 1 - 14 of 14 Answers
g.soma shekar

Answered On : Dec 15th, 2005

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.

  
Login to rate this answer.
umesh choubey

Answered On : Jan 9th, 2007

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
adarsh83

Answered On : May 10th, 2007

View all answers by adarsh83

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

  
Login to rate this answer.
Sunny

Answered On : Jul 8th, 2007

Every body is just explaning what is that........ but what about how is that?

  
Login to rate this answer.
Bharath

Answered On : Oct 15th, 2007

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.

  
Login to rate this answer.
omkarpatil83

Answered On : Mar 13th, 2008

View all answers by omkarpatil83

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

  
Login to rate this answer.
Tins30

Answered On : May 29th, 2008

View all answers by Tins30

Interface provides method signature and not the body. Its up to the developers toprovide 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 andevery 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 canimplement from more than one interface. Not same with Abstract. One can inheritfrom 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.ThanxTins30

  
Login to rate this answer.
Tins30

Answered On : May 30th, 2008

View all answers by Tins30

"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 youstated for interface.. Plz correct me if I am wrong.ThanxTins30

  
Login to rate this answer.
Karuna Reddy

Answered On : Jun 1st, 2008

View all answers by Karuna Reddy

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 FutureAbstarct:1. when it necessay dont create the instance of the class for the security purpose.

  
Login to rate this answer.
Tins30

Answered On : Jun 3rd, 2008

View all answers by Tins30

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. RightPoint 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.

  
Login to rate this answer.
Nidhees

Answered On : Jul 23rd, 2010

View all answers by Nidhees

The operation of abstract class is same as interface but a minute difference between them. when we want to apply all the functionality of class or interface in the extended or implemented class or interface, then in that condition, we use interface for example if we have two method like getName() and getAddr() in the interface and we define(override) only one method like getName() or getAddr() then JVM will report compile time error untill we define those two methods(getName() & getAddr()).But if we are using abstract class then iabstract class  provides flaxbility to the user because if those two methods say 'getName() and getAddr()' defined in abstract class,then we extends that abstract class and as per our need override any one or both the methods.except that if we want to operate on java classes using object then we use abstract class else interface.Nidhees Kumar Singh(Kalwari,Balrampur)

  
Login to rate this answer.

When i have to extends a class, then in that case it is better to use Interface. Suppose that i have a  class Product, and i have to extends class Customer into this class. So if i have extended a Abstract class earlier, in that case i would have not be able to  again extends this Class Customer, because Java does not give facility of the Multiple inheritance. So to implement the multiple inheritance in out code we use Interfaces over the Abstract class.

  
Login to rate this answer.
aparna

Answered On : Dec 19th, 2011

If you want common functionality(generalization) for some classes go for abstract
If you dont want generalization then go for abstract class

  
Login to rate this answer.
seenthil

Answered On : Mar 25th, 2013

1. If you are creating something that provides common functionality to unrelated classes, use an interface.

2. If you are creating something for objects that are closely related in a hierarchy, use an abstract class.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.