Interface vs Abstract Class
Abstract classes
Abstract class is class which contain one or more abstract methods, which is implemented by sub classes. An abstract class can contain no abstract methods but also containe mehtod with body. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses.Abstract class can contain private as well as protected members.
When to use Abstract Class-If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality.
Interface
Interface is extremely useful when you don’t want a big hierarchical type framework. As interfaces are implicitly abstract, they cannot be directly instantiated except when instantiated by a class which implements the said interface. The class must implement all of the methods described in the interface, or be an abstract class. An Interface can only have public members. A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.Maintainability–if you want to add a new feature (method) in its contract, then you must implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class.
When To use interface – Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality.It is used where there of chances adding new method in future. Interfaces are more used to set standards. interface gave merely a specification,nothing implemented for any standalone project which can be changed at will its more design flexible and it can be utilized to model multiple inheritance.
Ref – http://www.interview-questions-java.com/java-questions/java-abstract-class-and-interface-interview-questions
Read more post on Abstract Interface Java Oracle ADF when to use abstract class when to use interface




Comments
The exact same explanation applies to .Net too.
Good Article.
Exactly, explanation applies to all oops languages. I must appreciate the clear line which vinay has drawn between the interfaces and abstract classes. Good work
Hi iskorn,
Well that is not misleading text.It depend on your perception.my understanding is that reader have some basic knowledge on interface and abstract class.Yes I am newbie and trying to share my knowledge becuase may be some of our friend who don’t have this much knowledge and who is more newbie then me.May be it can help them. NOM
Abstract classes are fast — This is perfectly true.Because Interface are slow in terms of speed. because it requires extra indirection to find the corresponding method in the actual class.
This my meaning.please correct me if i am wrong.
Hi Gonzalo,
Yes it is applicable to all OOPs lanauages.
To HMM, — It is not misleading .my understanding is that reader have some basic knowledge on interface and abstract class.I try to give explation upto the point…….
Well, if we’re talking about JVM there is a difference between invokeinterface and invokevirtual commands. However from the performance point of view it is negligible (if exists) in a long run – see the test below. Please note the JVM performs optimization of such calls, so it is not a good idea to consider it (if you have more than basic knowledge of the subject).
package test;
interface Interface {
void method();
}
abstract class AbstractClass {
public abstract void method();
}
public class Test { // run with -server JVM option
private static long count1 = 0, count2 = 0;
private static final int TOTAL = 200000000;
public static void main(String[] args) {
Interface var1 = new Interface() {
public void method() {
count1++;
}
};
AbstractClass var2 = new AbstractClass() {
@Override
public void method() {
count2++;
}
};
for(int i = 0; i < TOTAL; i++) { // allow JV< to optimize
var1.method();
}
long start1 = System.nanoTime();
for(int i = 0; i < TOTAL; i++) {
var1.method();
}
System.out.println(System.nanoTime() – start1);
for(int i = 0; i < TOTAL; i++) { // allow JVM to optimize
var2.method();
}
long start2 = System.nanoTime();
for(int i = 0; i < TOTAL; i++) {
var2.method();
}
System.out.println(System.nanoTime() – start2);
}
}
I agree with Urs Enzler, interfaces give you far more flexibility (for example by the use of the Dependency Injection pattern). Furthermore the performance is always a debate. Interfaces keeps the software manageable.
Btw what has this topic to do with Service Oriented Architecture? Maybe include Abstract Class vs Interface vs Service Contract?
Abstract classes are not faster than interfaces. Only
final methods could be faster depending on JVM implementation.
@sagar
Can u give us the correct version of the statement u are saying as wrong??
Well i am surprised to see comment of Mr Sagar. Where does this article is mis-leading. I agree this article doesn’t contain all the information about abstract & interface, IMO it is not possible to cover every single aspect of any technology in single blog post, but It doesn’t mean it is contain some “superficial knowledge”.
i think you should appreciate vinay’s effort in putting up all this.
If you think information is “superficial knowledge” please post a link here. I think Vinay would be happy to update the post.
Thanks for the nice article and I absolutely agree with Jetspeed’s statement. Those who think something is misleading shud prove their way of thinking else stop spamming.
interface describes “What to do” but does not have anything like “How to do”. It is the responsibility of implementer to how to do.
In early stage of your design, You are not clear with how to implement the things but you are very much sure about what to do. So interface should be used.
In case you are some how clear with the implementation then use abstract class where you make concrete methods for which you know hoe to implement, and keep other methods as abstract to let derived class to implement them.
Trackbacks