Thursday, October 3, 2013

Difference between Abstract class and Interface

One more common interview question from the Java interview bucket.

Both conceptually doing the same work but with few differences. Its up to the programmer how he / she wants to use it and when. Here I have mentioned few of their major differences with example.

Declaration
Abstract class is declared by using abstract keyword along with the class signature.
public abstract class AbstractTest {
}

Interface is declared by using interface keyword.
public interface InterfaceTest {
}

Class Members
Abstract class members can be public, private, protected or default. But Interface members are public by default.
public interface InterfaceTest {
private String testString
}

The above code will result in compilation error with message: Illegal modifier for Interface. Because it only allows public, static and final access specifiers.

Variable declaration
Interface can have only constant variables which are by default public static final where as Abstract class can have non-final variables as a normal class.

public interface InterfaceTest {
String testString = "ABCTEST";

}

The above code will work fine without any compilation error. The variable declaration is equal to
public static final String testString = "ABCTEST" by default.

Methods
Only abstract methods are allowed inside an interface. Abstract class can contain concrete methods with abstract methods.
Abstract method: Methods without any body, only method declaration.
Concrete method: Methods with body / implementation inside it.

Multiple Inheritance
A class can implement more than one Interface but a class can only extend one abstract class at a time. After extending one class you can't extend any other class.

Absolutely abstract
Why Interface are called absolutely abstract ? Because, Interface can not have main method  (public static void main(String[] args)) declared inside it. It can't be instantiated by any chance.

An Abstract class can have a main method where you can use it as a normal Java class to execute. But it can't instantiated outside the abstract class.

Hope this article help you to understand the difference between abstract class and interface. You can post your questions or suggestions in the comment section.

2 comments:

Anonymous said...

An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class sub-classing from it, it is said to implement that interface.

John said...

A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.