wood burning stoves 2.0*
The moose likes Jobs Discussion and the fly likes Interview Question: interface vs abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Careers » Jobs Discussion
Bookmark "Interview Question: interface vs abstract class" Watch "Interview Question: interface vs abstract class" New topic
Author

Interview Question: interface vs abstract class

prat de
Greenhorn

Joined: Sep 15, 2003
Posts: 22
*** Subject modified to make it clear that this was an interview question. --Mark ***
why to use Interface in java if we can accomplish the same thing by using Abstract Class...in terms of defering implementation of methods..this was one of the question asked to me during java interview...
please let me know if any of my friends here can answer this question..
- prat.
[ September 15, 2003: Message edited by: Mark Herschberg ]
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
You'll probably get a better response if I move this to the beginners forum. Still, because intervew questions of any topic are allowed in the forum, I will let it stay here if you wish.
--Mark
Andrzej Filusz
Greenhorn

Joined: Apr 22, 2003
Posts: 6
Originally posted by prat de:

why to use Interface in java if we can accomplish the same thing by using Abstract Class...

Hi!!!
I think that you have problem with the right answer for this question just because it's a little tricky one. In my opinion, this the part of this question after 'if' is wrong. For example, one class can extend only *one* another class (this class my be abstract like in this case) BUT it can implement *several* interfaces.
Although Java does not support multiple class inheritance, we can get something a little similar using interfaces (but not the same of course).
[ September 16, 2003: Message edited by: Andrzej Filusz ]

Regards,<br />Andrzej Filusz (Poland)<br />SCJP2 1.2
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I believe this is a popular interview question because it is the type of question that gives the interviewee the opportunity to share some of their knowledge on the subtleties of the Java language.
We had a question that was designed to invoke a similar response. It was something like "which is better, custom built or proprietary products/packages/components". There is no right or wrong, it's about seeing how far the interviewee can think into the question.
In the original question, it gives the interviewee the opportunity to talk about designing behaviour over data, behaviour abstraction, programming to interfaces, frameworks, and anything else that strikes their fancy.
D. Rose
Ranch Hand

Joined: Apr 25, 2003
Posts: 215
When you use abstract class for defering implementation you are subclassing from that class and committing to inheritance (implying 'is a' relationship).
With interface there is no such binding. Interface allows for polymorphism.
Given that java does not allow mutiple inheritance, I think chosing between abstract class and an interface has to be done based on this and not just becasue it can be done other way round. It will be bad design.
e.g suppose I have a 'cycle' object. I want to implement 'run' functionality for it. If I have already have Animal abstract class ( and related subclasses in my system) with 'run' method, it will be illogical to subclass 'cycle' from it.
Yes, you can define a 'Vehicle' abstract class and subclass cycle from it.
Now you can define 'run' again in 'vehicle' class hierarchy but best will be to define 'run' as an interface and let both 'vehicle' and 'Animal' implement it in their own fashion.
Now you can even have 'Tiger' as your 'Vehicle' if you want (because it can 'run')!
I hope you get the point.
[ September 16, 2003: Message edited by: Rei Damle ]
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
Rei,
When one uses inheritance to reflect relationship as against containment, isn't he/she committing to is-a relationship , irrespective of interface or abstract class ?
In the example you have given, Tiger 'is-a' Vehicle where Vehicle has method run(), right ?
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
Inheritance is an is-a relationship. A slight peep into the multiple-inheritance implementation in C++ will show you how complicated the whole thing is.
Java doesnot have multiple inheritance because of, apart from many other reasons, the complexity of implementing the same and the ambiguities you would face implementing it.
Interface vs. Abstract Classes
The both represent inheritance. One is a behavioural inheritance and the other inherits states. If abstract classes donot implement any state parameters, they too represent behavioural inheritance.
Inheritance is an IS A relationship and inheritance has nothing to do with polymorphism. Languages like SmallTalk allow you to implemented polymorphism between unrelated objects and particularly between objects which are not within the same hierarchy. Polymorphism is just a method compatibility.
prat de
Greenhorn

Joined: Sep 15, 2003
Posts: 22
Thanks for your replies/answer and clearing my doubt regarding interfaces & abstract classes.
- prat.
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
Martin Fowler and Kendall Scott address this issue in UML Distilled, Addison Wesley, p. 88 first Ed.

There is no distinction between refining an interface and subclassing and abstact class.
Abstract classes and interfaces are similar, but there is a difference. Both allow you to define an interface and defer its implementation until later. However, the abstract class allows you to add implementation of some of the methods; an interface forces you to defer implementation of all methods.

I gave a similar answer in an interview. The guy shook his head at me like I was an idiot.
I can't help thinking about Back to School when Rodney Dangerfield hires Kurt Vonnegut to write a paper about a Kurt Vonnegut novel. His prof says he's an idiot and doesn't know anything about Vonnegut.
:roll:
Al Newman
Ranch Hand

Joined: Mar 30, 2003
Posts: 716
Simple. There has to be a compelling reason to use inheritance, typically because one or more methods have been implemented at the parent level.
Otherwise always use an interface.


SCJP1.4, SCWCD
 
GeeCON Prague 2014
 
subject: Interview Question: interface vs abstract class