Sunday, May 23, 2010

Best Strategy ? Abstract classess Or Interface

Few points before to proceed :

1) Abstract class can have one or more abstract methods as well as concrete methods.
2) An abstract class can't not be instantiated.
3) To use functionality of abstract class One can create a concrete class that has to instantiate the abstract class and must have implementation for each abstract method.
4) Abstract classes are usually declared where two or more subclasses are expected to fulfill a similar role in different ways through different implementations.
5) Interfaces defines the behavior of the classes. in another word Contract.
6) All methods declared in interface are abstract by default, no need to use abstract keyword.
7) Basic feature of OO is Encapsulation : Reveal an object's programming interface (functionality of the object) without revealing its implementation.
8) Interfaces have no direct inherited relationship with any particular class, they are defined independently. Interfaces themselves have inheritance relationship
among themselves.

When Should Interfaces or Abstract classes some point are below :
First and very important is "Choice of design".
Second there are some facts and features of each, according to design we should use them.

1) Using Interfaces, The implementation can change without affecting
the caller of the interface.
2) Using Interfaces, Unrelated (One class is not a sub-class of another class) classes can implement similar methods(behaviors).
3) An interface can only define constants while abstract class can have fields.
4) Use Abstract classes, where you want to provide common implementation code for all its sub classes. It reduces the duplication. However we should not forget that a concrete can extend only one super class whether that super class is in the form of concrete class or abstract class.

I would use interfaces when I think that something there are very frequent changes in my design. How??? Simple example :

Consider an interface that you have developed called "TaskHandler":

public interface TaskHandler{
void performSomething(int i, float x);
int performMore(String s);
}


Now after some time , you want to add a third method to "TaskHandler", so that the interface now becomes:

public interface TaskHandler{
void performSomething(int i, float x);
int performMore(String s);
boolean willYouPerform(int i, float x, String s);
}


Points to be noted:

If you make this change, all classes that implement the old TaskHandler
interface will break because they don't implement all methods of
the the interface now.

Solution :
Create one more interface OtherTaskHandler that extends TaskHandler interface

public interface OtherTaskHandler extends TaskHandler {
boolean willYouPerform(int i, float x, String s);
}


Now Its up to users, he/she can choose to continue to use the old interface or to upgrade to the new interface.

Tuesday, May 18, 2010

Java is not a Pure Object Oriented Language






Theoretical a Pure Object Oriented Language must contains all entities as in the form of Object. There could be two categories of OOPL :
1) Hybrid OO languages :
Hybrid languages are based on some non-OO model that has been enhanced with OO concepts. e.g. C++, ancestor was C, may be more...

2) Pure OO languages
Pure OO languages are based entirely on OO principles; Smalltalk, Java, and Simula are pure OO languages.

But Due to following reasons Java can't be considered as Pure OOPL:
1) Java violates principle "Everything must be Object". Java has primitive types int, long etc.
2) static works on class level.
3) Multiple inheritance is not supported.
4) Operator overloading is not supported.

BTW Java never claimed to be "Pure OOPL".