Thursday 11 July 2013

Difference between Abstract classes and Interfaces

Difference between Abstract classes and Interfaces E-mail


Abstract classes
Interfaces
Abstract classes are used only when there is a “is-a” type of relationship between the classes.
Interfaces can be implemented by classes that are not related to one another.
You cannot extend more than one abstract class.
You can implement more than one interface.
Abstract class can implemented some methods also.
Interfaces can not implement methods.
With abstract classes, you are grabbing away each class’s individuality.
With Interfaces, you are merely extending each class’s functionality.

 What is difference between interface and abstract class in Java

What is abstract class and interface in Java
Difference between abstract class and interface in Java is one of the tricky Java interview question and mostly appear in core Java interviews. Both interface and abstract class is a way to achieve abstraction in Java but there are significant difference between both of them. some time interviewer also not just focus on key differences between abstract class and interface in Java but also interested in When to use interface in Java and When to use abstract class is Java, This is a tricky pat of this interview question and  you must have good understanding of What is interface and abstract class in Java and How to use them. Anyway in this Java article we will first see some syntactical difference between interface and abstract class in Java programming language and later we will see where to use abstract class and interface
.

Abstract class vs interface in Java

In last section we saw what is abstract class and interface and now let's see difference between interface and abstract class in Java.

1) First and major difference between abstract class and interface is that, abstract class is a class while interface is a interface, means by extending abstract class you can not extend another class because Java does not support multiple inheritance but you can implement multiple inheritance in Java.

2) Second difference between interface and abstract class in Java is that you can not create non abstract method in interface, every method in interface is by default abstract, but you can create non abstract method in abstract class. Even a class which doesn't contain any abstract method can be abstract by using abstract keyword.

3) Third difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java. This is not a significant difference in most of cases but if you are writing a time critical application than you may not want to leave any stone unturned.

4) Fourth difference between abstract class vs interface in Java is that, interface are better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective.

5) Another notable difference between interface and abstract class is that when you add a new method in existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good. By using abstract class you can provide default implementation in super class.


That's all on difference between abstract class and interface in Java, I will add more differences whenever I learn new things.


They are quite similar but there are some important technical differences:
  • An abstract class allows you to provide a default implementation for some of the methods but an interface does not allow you to provide any implementations.
  • You can implement multiple interfaces but you can only inherit from one abstract class.
These differences affect how the two techniques should be used:
  • You should use an interface to define a contract.
  • An abstract class can be useful to reuse code... but be aware that it is not the only way to reuse code. You should also consider other approaches such as containment.
"Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.

Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..

Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

A Java class can implement multiple interfaces but it can extend only one abstract class.
Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.

In comparison with java abstract classes, java interfaces are slow as it requires extra indirection."

Source(s):

No comments:

Post a Comment