Usage of Abstract Classes I could give you a practical Example. We could use Abstract Classes when the functionality can be more or less decided. so you could implement these methods in advance and then provide "fill in the blanks" to the developer of the concrete subclass of this Abstract class.
I used a similar approach in developing a Product Catalog, which has a "pre-Loaded" Data base in a number of Hashmaps, with keys being the PKs of each row and the Value being a Java Bean format Data Base Object.
I created a "AbstractProductCatalog" which has declared fields of Hashmaps initialized to null. and all implemented System interface APIS like "getProductById(int nProdId)" etc. the Abstract class knows that the "PRODUCT_CACHE" contains the Product Data, so the implemented getProductById would query this Hashmap.
Now I force the Sub classer to "fill" the Product Hashmap through a abstract method. "public abstract void initProductCatalog();" ( or more detailed Methods which would fill each Business Object.
Usage of Interface This is a very tricky one, as interfaces are themselves not usable / instantiable, they can be used to achieve goals to make an system more adaptible and also more reusable.
some scenarios:
1.
A class if it implements an interface can represent an Object another system expects with a simple cast. this will be useful when
we want to make "Plug and Play" possible. example : we do this when we want to serialize an Object. all we need to do is that we implement the method Serializable and the JVM needs only to know if this class is serializable or not.
2.
Interfaces can be used as the "messengers" between two systems, where the receiving system need to know only the interface and not have the whole other systems library in its path all you need is some middle tier code ( generally using reflection so that it could inturn look in the same VM or a different VM) and request the interface. ( I tried to do give an example below )
3.
Simply you could define interfaces and let the providor implement all the Logic, all you do is know how to get that interface and start using the interfaces. a good example is the SQL API, where all the APIS are interfaces and they are implemented by the Database vendors or some third parties. so no wonder what Database you code to, as long as you are using the SQL API and you have a driver you can do Database Access coding in your applications.
Example:
Lets say that there is a new requirement to provide a GUI front end to the same Product Catalog that we created above. so in order to make best the concept of re-usability, we could now create a new interface called AbstractProdCatalogIntf so that the client can be made very thin and need to have only the Java Beans (getters and setters for an entity) and the interface in its path.
public interface AbstractProdCatalogIntf
{
// declare all the methods like
public Product getProductbyId(int nProdId) throws BusinessException;
public void initialize();
}
//Then make the AbstractProductCatalog implement the Interface with the other things being same.
now I create a new application which Extends JFrame.
public ProdCatFrontEnd extends JFrame
{
AbstractProdCatalogIntf aInterface = someGenericClassUsingReflection.getProductCatalog();
//start Using the interface
}
}
Note that the implementation of the interfaces is not very clear...but I hope it atleast tries to hit the point. And Sorrz if I created too much confusion