*** 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 ]
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 ]
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.
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 ]
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 ?
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.
Joined: Sep 15, 2003
Thanks for your replies/answer and clearing my doubt regarding interfaces & abstract classes. - prat.
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: