aspose file tools*
The moose likes Java in General and the fly likes Interface vs fully abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface vs fully abstract class" Watch "Interface vs fully abstract class" New topic
Author

Interface vs fully abstract class

Bala Gangadhar
Ranch Hand

Joined: Oct 07, 2008
Posts: 80
    
    1
Hi All,
While I was giving interview I was asked...

Why can't we replace the interface with fully abstract class (i.e. abstract class with all methods as abstract). I was asked what's the advantage of using interface in this case instead of
fully abstract class.

I could not give a proper answer for the same..
Can somebody give me a proper reason for the same.


Regards,
Bala.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
A class can only extend one other class, but it can implement any number of interfaces.
Bala Gangadhar
Ranch Hand

Joined: Oct 07, 2008
Posts: 80
    
    1
I have answered the same.... but the interviewer expecting something else from me....
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I wonder what else might he have expected..
That answer is the bulls eye!


OCPJP 6
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Also... In interface we dont have to explicitly declare as "public abstract" for methods as abstract classes.

In that context, going for fully abstract class which contains only abstract methods, interface is good...

If that fully abstract class "has an" object reference to other class, fully abstract class is best..


SCJP 6 [SCJP - Old is Gold]
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
The answers above are right. But not the reason. Abstract classes have their reason for being in the language. So do interfaces.

Abstract classes are those classes which are not ready yet. They have a few methods a few variables but still are not ready. Thus you cannot make objects of these classes. Some examples of places where your design would hav abstract classes would be if you are making a platform independant file access class with your own implementation of the buffer and io management. The methods which do the buffer and io management (if you want to write them per platform), each platform will havei its own implementation. So a subclass which derives from your class will simply write the methods and start working. But without these, you cannot instantiate your class. Hence your class is abstract.

Or in terms of text book examples, an automobile is an abstract class. It can drive , honk etc. How it does this depends on which subclass of the automobile you look at.


One of the places where interfaces are used is where you have certain requirements which a particular object which you use in your class has to meet. You are not writing the code for this class. You are using the object. But you need it to perform a few tasks. You can then define these tasks in an interface. The creator of this class will then implement your interface and gurantee you that your requirements with respect to the methods has been met.


Since both end up having a similar contract, they both can be used in each others place at times. But bear in mind they have different reasons for being in the picture.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Gaurav... Super Explanation...

As an add-up to his explanation, Interfaces can be implemented by different classes of different hierarchy...
That is ,

There would be some common behaviors among the two irrelevant classes... To connect these classes, common behaviors are defined in interfaces... So Interface connects the two different classes...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

interface always contain implicitly final variables but abstract classes donot have final variable.


SCJP6.0,My blog Ranchers from Delhi
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A Java Interface class ISA a fully abstract class. It is a certain type of abstract class. And when you compile the source code for an interface class, you will get a "class" file.

And when you try to run an application that needs this "class" on the classpath and its not there, you will get a "class" not found exception.

Hope this helps
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

The only difference being that you can implement multiple interfaces and extend only one class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Rob Prime wrote:The only difference being that you can implement multiple interfaces and extend only one class.


Great
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface vs fully abstract class
 
Similar Threads
Abstract classes and Interfaces in Project
Interface methods
when should i use abstract class and when i should i interfaces
Interface and Abstract Classes
Examples For Typical Use of Abstract Classes and Interfaces