Friday, July 3, 2015

Abstract Class VS Interface

Abstract Class:
Abstract Class in Java cannot be instantiated. In Other words an abstract class is a class which cannot be instantiated. In Object Oriented Design these Classes are used to store methods that are common to the subclasses that is going to be inherited from it, so that they can be available to them.
Abstract Methods can be defined in abstract classes. Abstract methods do not have implementation but are forced to implement in their subclasses.

Interface:
Interface has all its methods abstract so basically is a collection of abstract classes. It does not contain any implementation of its method and hence all methods in Interface needs to be Implemented by Inheriting Class unless the inheriting class is an abstract class. I case of abstract class inheriting interface it can again leave some or all unimplemented methods abstract which will then be implemented in  a class inheriting that abstract class.
Interface is implicitly abstract, all its methods are implicitly abstract and public.

Difference between Abstract Class and Interface in Java 7:
1. Abstact class can have both abstract and non abstract methods but interface can only have abstract.
2.Abstract does not allow multiple inheritance but Interface does.
3.Abstract can have static,non static, final and non final variables but Interface can only have Static and Final Variables.
4.Abstract class is inherited but Interface are implemented (Interface is not even a class).
5.Abstract Class can have private, protected and public methods but Interface can have only public.

Similarities Between Abstract Class and Interface:
1. Both cannot be instantiated.
2. Both can be used for Polymorphism.

Example of Interface and abstract Class used together effectively:
List is an example of how interface and abstract classes can be used together effectively.

Advantage of Using Interface:
1.Multiple Inheritance.
2.Clear Picture of all implementation provided.
3.Interfaces function to break up the complex designs and clear the dependencies between objects.
4.Interface makes application loosely coupled

Program to Interface P2I:
This Principle says we should always use abstract class for accomplishing some behavior, It doesn't care what concrete object you use, it just cares that it implements certain behavior. This makes the class much more flexible, as it can be provided with any number of concrete implementations to do its work.

This principle is really about dependency relationships which have to be carefully managed in a large app. – Erich Gamma
To put it in other words, you have to follow the OO concept of Encapsulation; the exposed part of a class is its interface. The important part here is that the interface represents “what” the class can do but not “how” it will do it, which is the actual implementation.

why follow this principle:
By Following this principle we ensures that dependancy to some super class or interface will ensure the implementation of function if there is some change in implementation for some requirement then only that subclass will be changed and or extended and hence others need not worry about the back ground details of all implemetations hence incapsulating the details. This Concept makes program extension manage able.

Evolving API problem:
If a method signature is added to a Interface after its implementation on client. All the subclass implementing this API will have an unimplemented method hence creating error for the user of API and breaks the code of Client which was perfectly working before adding the method signature. This problem is known as Evolving API problem.

No comments:

Post a Comment