Thursday, September 25, 2014

Interface Vs Abstract class

Either preparing an exam or an interview this is a must know. Be aware and don't be the next victim. As stated in wikipedia. Starting with Java 8 default and static methods may have implementation in the interface definition. Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class.
           However an Abstract class is a class that represents a generalization and provides functionality but is only intended to be extended and instantiated. Without going too far into unecessary theories, I would like to share with you this  comprehensive comparison that I found on DURGA SOFT
INTERFACE ABSTRACT CLASS
If we have the requirement specification and we don't know anything about the implementation In case of partial implementation, here we are talking about the implementation but not completely
Inside interface every method is always public and abstract whether we are declaring or not. Hence interface is also considered as 100% pure abstract class Every methods present in abstract class need not to be public or abstract. In addition to abstract methods we can take concrete methods also.
We cannot declare interface methods with the following modifiers: private, protected, final, static, synchronized, native, strictfp There are no restriction on abstract class method modifiers
Variables are always public, static and final whether we are declaring or not. Variables need not to be public, final and static
Variables cannot be declared with the following modifiers: private, protected, volatile, transient No restrictions on abstract class variables modifiers
Variables must be initialized at declaration otherwise compile time error Variables initialization not required at declaration time
Not possible to declare instance and static blocks otherwise compile time error Instance and static blocks allowed
No constructors allowed Constructor declaration allowed and to be executed during child objects creation

No comments:

Post a Comment